Heartbeat does not fire as expected (frontend)

I want to use the wordpress heartbeat functionality, because I want to detect if a user is still on the site / on my custom post type site.
(Because I want that only one user can be at my custom post type site at the same time).

So I have included the heartbeat.js in my frontend over

wp_enqueue_script(‘heartbeat’);

and added some information to the heartbeat in my Javascript file

jQuery( document ).on( ‘heartbeat-send’, function ( event, data ) {
// Add additional data to Heartbeat data.
var customPostId= jQuery(“#customPostId”).val();
if (typeof customPostId != typeof undefined && customPostId != ”) {
data.myCustomPostId = customPostId;
}
});

When the heartbeat isn’t sended for 3 Minutes, I assume that the user is no longer on the page or has closed the browser.

The heartbeat interval is on 60 seconds. (event if the page is focused or not)
But when I look at my log file (or look at the developer tools of firefox), I see that the heartbeat is sended every 120 seconds.
And sometimes, the heartbeat needs 4 Minutes (or more) to get send.

So, when the heartbeat needs 4 Minutes (or more) to get send, my check fails.

Is there any way to force the heartbeat? Or am I doing something wrong? Or is there maybe a better way to check if the user is still on my page?

Read more here:: Heartbeat does not fire as expected (frontend)

Leave a Reply

Your email address will not be published. Required fields are marked *