0

I have a file named handler.php which reads data from a text file and pushes it to a client page.

Relevant client code:

<script>
if(typeof(EventSource) !== "undefined") {
    var source = new EventSource("handler.php");
    source.onmessage = function(event) {
        var textarea = document.getElementById("subtitles");
        textarea.value += event.data;
        textarea.scrollTop = textarea.scrollHeight;

    };
} else {
    document.getElementById("subtitles").value = "Server-sent events not supported.";
}
</script>

Handler.php code:

$id = 0;
$event = 'event1';
$oldValue = null;

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('X-Accel-Buffering: no');

while(true){

    try {
        $data = file_get_contents('liveData.txt');
    } catch(Exception $e) {
        $data = $e->getMessage();
    }

    if ($oldValue !== $data) {
        $oldValue = $data;
        echo 'id: '    . $id++  . PHP_EOL;
        echo 'event: ' . $event . PHP_EOL;
        echo 'retry: 2000'      . PHP_EOL;
        echo 'data: '  . json_encode($data) . PHP_EOL;
        echo PHP_EOL;

        @ob_flush();
        @flush();
        sleep(1);  
        }

  }

When using the loop, handler.php is never loaded so the client doesn't get sent any data. In the Chrome developer network tab, handler.php is shown as "Pending" and then "Cancelled". The file itself stays locked for around 30 seconds.

However, if I remove the while loop (as shown below), handler.php is loaded and the client does receive data (only once, even though the liveData.txt file is constantly updated).

Handler.php without loop:

$id = 0;
$event = 'event1';
$oldValue = null;

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('X-Accel-Buffering: no');

try {
    $data = file_get_contents('liveData.txt');
} catch(Exception $e) {
    $data = $e->getMessage();
}

if ($oldValue !== $data) {
    $oldValue = $data;
    echo 'id: '    . $id++  . PHP_EOL;
    echo 'event: ' . $event . PHP_EOL;
    echo 'retry: 2000'      . PHP_EOL;
    echo 'data: '  . json_encode($data) . PHP_EOL;
    echo PHP_EOL;

    @ob_flush();
    @flush();

    }

I'm using SSE as I only need one-way communication (so websockets are probably overkill) and I really don't want to use polling. If I can't sort this out, I may have to.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Flushing the output doth not a complete request make. You've still got an infinite loop. Send the output once and exit, then do the looping on the client side. – Alex Howansky Oct 04 '18 at 15:49
  • Then I'd be polling, Alex, which is not what I want. The infinite loop is to continually send data and it should work - see RamRaider's answers below. – Mark Raishbrook Oct 04 '18 at 17:47

4 Answers4

3

The client side of the SSE connection looks OK as far as I can tell - though I moved the var textarea..... outside of the onmessage handler.

UPDATE: I should have looked closer but the event to monitor is event1 so we need to set an event listener for that event.

<script>
    if( typeof( EventSource ) !== "undefined" ) {
        var url = 'handler.php'

        var source = new EventSource( url );
        var textarea = document.getElementById("subtitles");


        source.addEventListener('event1', function(e){
            textarea.value += e.data;
            textarea.scrollTop = textarea.scrollHeight;
            console.info(e.data);
        },false );

    } else {
        document.getElementById("subtitles").value = "Server-sent events not supported.";
    }
</script>

As for the SSE server script I tend to employ a method like this

<?php
    /* make sure the script does not timeout */
    set_time_limit( 0 );
    ini_set('auto_detect_line_endings', 1);
    ini_set('max_execution_time', '0');

    /* start fresh */
    ob_end_clean();


    /* ultility function for sending SSE messages */
    function sse( $evtname='sse', $data=null, $retry=1000 ){
        if( !is_null( $data ) ){
            echo "event:".$evtname."\r\n";
            echo "retry:".$retry."\r\n";
            echo "data:" . json_encode( $data, JSON_FORCE_OBJECT | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS );
            echo "\r\n\r\n";
        }
    }



    $id = 0;
    $event = 'event1';
    $oldValue = null;

    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    header('X-Accel-Buffering: no');





    while( true ){
        try {
            $data = @file_get_contents( 'liveData.txt' );
        } catch( Exception $e ) {
            $data = $e->getMessage();
        }

        if( $oldValue !== $data ) {

            /* data has changed or first iteration */
            $oldValue = $data;

            /* send the sse message */
            sse( $event, $data );

            /* make sure all buffers are cleansed */
            if( @ob_get_level() > 0 ) for( $i=0; $i < @ob_get_level(); $i++ ) @ob_flush();
            @flush();           
        }



        /* 
            sleep each iteration regardless of whether the data has changed or not.... 
        */
        sleep(1);
    }



    if( @ob_get_level() > 0 ) {
        for( $i=0; $i < @ob_get_level(); $i++ ) @ob_flush();
        @ob_end_clean();
    }
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Thanks for the suggestion. I shall give that a try. – Mark Raishbrook Oct 04 '18 at 16:10
  • Same behaviour, I'm afraid. – Mark Raishbrook Oct 04 '18 at 16:32
  • I set this up as a test locally and, after changing the event listener, it worked perfectly well. [ Client on Pastebin: https://pastebin.com/bsR5Qnm1, Server on Pastebin: https://pastebin.com/7uXSA9Zc ] – Professor Abronsius Oct 04 '18 at 16:45
  • No change, unfortunately (but many thanks for your efforts). The handler file was pending then cancelled after three minutes. This is soooo frustrating! – Mark Raishbrook Oct 04 '18 at 17:49
  • try running my two ( pastebin entries ) as client and server as a pure test - my guess is that it has something to do with reading the textfile – Professor Abronsius Oct 04 '18 at 20:29
  • Here's a weird thing. If I comment out the sleep(1) statement in the server-side loop, the client receives data (the page becomes unresponsive, of course). However, that only works if the data source is $data = rand(0,1). If I set the text file to be the data source, I get the 3.7 minute pending and subsequent cancellation again. – Mark Raishbrook Oct 04 '18 at 21:35
  • any chance you can make a paste to show your client & server implementations? as you suspect this might be host related if your code is identical to the code above ~ I'm running a modified version just now as a long term test and it's working 100%ok thus far – Professor Abronsius Oct 05 '18 at 08:03
  • My test code is identical to your Pastebin code, RamRaider. I'm going to contact Vevida and will report back. – Mark Raishbrook Oct 05 '18 at 09:03
  • Success, RamRaider! Please see my update above. Many thanks for all your help. My reputation doesn't allow me to up vote you, so please accept a virtual +1 instead! – Mark Raishbrook Oct 05 '18 at 13:47
  • Good to hear - what lead to the final resolution? – Professor Abronsius Oct 05 '18 at 15:37
  • Removing the loop in the php code. I updated my original question to show what I did. Thanks again! – Mark Raishbrook Oct 05 '18 at 15:58
1

When using the loop, handler.php is never loaded so the client doesn't get sent any data. In the Chrome developer network tab, handler.php is shown as "Pending" and then "Cancelled". The file itself stays locked for around 30 seconds.

This is because the webserver (Apache) or the browser or even PHP itself cancel the request when there is no response within 30 seconds.

So I guess the flushing does not work, try to actively start and end the buffer without using @ functions so you get a clue when there is an error.

// Start output buffer
ob_start();

// Write content
echo ''; 

// Flush output buffer
ob_end_flush();
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
0

I think you have a problem with the way the web works. The PHP code doesn't run in your browser - it just creates something that the web server hands off to the browser over the wire.

Once the page is loaded from the server that's it. You will need to implement something that polls for changes.

One way I've done this is to put the page in a loop that refreshes and therefore fetches the page again with the new data every second or so (but this could seriously overload your server if there's a lot of folks on that page).

The only other solution is to use push technology and a javascript framework that can take the push and repopulate the relevant parts of the page, or a javascript loop on a timer that pulls the data.

Ghoti
  • 2,388
  • 1
  • 18
  • 22
  • @Goti Thanks for the answer. I *am* attempting to use push technology, hence the EventSource code in the client which references the php file as per the examples at https://www.w3schools.com/html/html5_serversentevents.asp. Refreshing the page (which I've used in the past) is not a solution. – Mark Raishbrook Oct 04 '18 at 16:08
  • The push needs to happen somewhere else - as a a daemon. The page being rendered should just be static and listen. They are different processes. – Ghoti Oct 05 '18 at 13:22
  • I've got it to work. Please see my update above. handler.php pushes and client.html listens. – Mark Raishbrook Oct 05 '18 at 13:58
0

(Posted solution on behalf of the question author).

Success! While debugging for the nth time, I decided to go back to basics and start again. I scrapped the loop and reduced the PHP code to a bare minimum, but kept the client-side code RamRaider provided. And now it all works wonderfully! And by playing around with the retry value, I can specify exactly how often data is pushed.

PHP (server side):

 <?php

    $id = 0;
    $event = 'event1';
    $oldValue = null;

    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    header('X-Accel-Buffering: no');

    try {
        $data = file_get_contents('liveData.txt');
    } catch(Exception $e) {
        $data = $e->getMessage();
    }

    if ($oldValue !== $data) {
        $oldValue = $data;
        echo 'id: '    . $id++  . PHP_EOL;
        echo 'event: ' . $event . PHP_EOL;
        echo 'retry: 500'      . PHP_EOL;
        echo "data: {$data}\n\n";
        echo PHP_EOL;

        @ob_flush();
        @flush();

        }

  ?>

Javascript (client side):

<script>
    if ( typeof(EventSource ) !== "undefined") {
        var url = 'handler.php'

        var source = new EventSource( url );
        var textarea = document.getElementById("subtitles");


        source.addEventListener('event1', function(e){
            textarea.value += e.data;
            textarea.scrollTop = textarea.scrollHeight;
            console.info(e.data);
        }, false );

    } else {
        document.getElementById("subtitles").value = "Server-sent events not supported.";
    }
</script>
halfer
  • 19,824
  • 17
  • 99
  • 186