Support » Developing with WordPress » Any idea why this heartbeat code works on desktop but not on mobile?

  • I have written some test code using the WordPress heartbeat feature and whilst it works perfectly as expected on Desktop, for some reason on my iphone the value does not update?

    My test use of WordPress heartbeat is very simple, it takes a number from the screen using jQuery, sends it off on the ‘heartbeat-send’ value in which a function then adds 1 to this number and then returns it back. As mentioned it all works perfectly on Desktop but not mobile (iPhone for me).

    jQuery on the page to handle the sending and receiving of heartbeat data:

    jQuery(document).ready(function(){
        // ON HEARTBEAT
        jQuery( document ).on( 'heartbeat-send', function ( event, data ) {
            // Add additional data to Heartbeat data.
            var matchID = jQuery('div.liveMins').html();
            console.log(matchID);
            data.match_id = matchID;
        });
    
        // ON HEARTBEAT RETURNED DATA
        jQuery( document ).on( 'heartbeat-tick', function ( event, data ) {
        // Check for our data, and use it.
        console.log('ping received');   
        if ( ! data.minutes_in_game ) {
            return;
        }
        jQuery('.liveMins').html(data.minutes_in_game);
    });   
    });

    And then this is my php function to process the data sent:

    function myplugin_receive_heartbeat( $response, $data ) {
        // If we didn't receive our data, don't send any back.
        if ( empty( $data['match_id'] ) ) {
            return $response;
        }
    
        $received_data = $data['match_id'];
    
        $response['minutes_in_game'] = intval($received_data) + 1;
        return $response;
    }
    add_filter( 'heartbeat_received', 'myplugin_receive_heartbeat', 10, 2 );

    So the above code works perfectly on desktop and I even added console logs to show that the correct data is being taken and sent etc, issue is mobile not working? The initial value loads on the page and then nothing changes even after waiting for many minutes despite the heartbeat being set to 16 seconds.

Viewing 1 replies (of 1 total)
  • Hello!

    I don’t have a definitive answer, but given that the code works in desktop browsers, my guess would be that it’s a quirk in iOS Safari. Have you tried using a different mobile browser (Chrome or Firefox perhaps) to see if that makes a difference? That would at least potentially narrow down the cause.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.