Are WordPress Heartbeat API ‘beats’ staggered or do they occur simultaneously for all users?

I would think the beats are Staggered by nature, becasue the next tick is determined by the browser time time() within the scheduleNextTick() method in the /wp-includes/js/heartbeat.jsfile:

 var delta = time() - settings.lastTick,
     interval = settings.mainInterval;

where it’s used to schedule the next tick with the setTimeout function:

if ( delta < interval ) {
    settings.beatTimer = window.setTimeout(
        function() {
            connect();
        },
        interval - delta
     );
} else {
    connect();
}

The browser time is defined as:

function time() {
   return (new Date()).getTime();
}

The connect() method contains the ajax call and uses always()

.always( function() {
    settings.connecting = false;
    scheduleNextTick();
})

to schedule the next tick.

The available tick intervals are 5s, 15s, 30s and 60s.

For a large number of very active users, with a short tick interval, the beats might seem to happen simultaneously.

It’s always good to have some data, so you could log the ticks from the logged in users, with the heartbeat_tick hook:

add_action( 'heartbeat_tick', 
    function(  $response, $screen_id  )
    {
        $file =  WP_CONTENT_DIR . '/ticks.log'; // Edit this filepath to your needs.

        if( file_exists( $file ) && is_writeable( $file ) ) 
        {
            file_put_contents( 
                $file, 
                sprintf( "%s - Tick from user_id : %d - from screen_id : %s" . PHP_EOL,
                    date( 'c' ),
                    get_current_user_id(),
                    $screen_id
                ), 
                FILE_APPEND | LOCK_EX 
            );
        }
    }
, 11, 2 ); 

Here’s an example from the ticks.log file:

2014-09-01T12:41:04+00:00 - Tick from user_id : 1 - from screen_id : edit-post
2014-09-01T12:41:19+00:00 - Tick from user_id : 1 - from screen_id : edit-post
2014-09-01T12:41:34+00:00 - Tick from user_id : 1 - from screen_id : edit-post
2014-09-01T12:41:56+00:00 - Tick from user_id : 1 - from screen_id : post
2014-09-01T12:42:11+00:00 - Tick from user_id : 1 - from screen_id : post
2014-09-01T12:42:20+00:00 - Tick from user_id : 3 - from screen_id : upload
2014-09-01T12:42:38+00:00 - Tick from user_id : 1 - from screen_id : post
2014-09-01T12:43:05+00:00 - Tick from user_id : 1 - from screen_id : post
2014-09-01T12:43:08+00:00 - Tick from user_id : 3 - from screen_id : attachment
2014-09-01T12:43:20+00:00 - Tick from user_id : 1 - from screen_id : post
2014-09-01T12:43:36+00:00 - Tick from user_id : 1 - from screen_id : post
2014-09-01T12:44:17+00:00 - Tick from user_id : 3 - from screen_id : profile

Leave a Comment