Switching off buffering for a php script

Discussion in 'Site Programming, Development and Design' started by John Atkins, Apr 29, 2017.

  1. Hi, not sure if this is allowed at winhost but I've been testing out Server Sent Events (SSE) from a php script and I'm finding that the usual php flush does not work. I only receive data in my browser (tried Chrome and Firefox) after the script terminates. I'm mainly trying to test the browser end of SSE so just wanted a simple php script at the server to send events at intervals. The data all floods in at the end rather than at intervals!
    Below is an example of the php script I've tried testing with.
    Thanks, John.
    Code:
    $string_length = 32;
    echo 'Begin test with an ' . $string_length . ' character string...<br />' . "\r\n";
    
    // For 3 seconds, repeat the string.
    for ($i = 0; $i < 3; $i++) {
      $string = str_repeat('.', $string_length);
      echo $string . '<br />' . "\r\n";
      echo $i . '<br />' . "\r\n";
      ob_flush();
      flush();
      sleep(5);
    }
    
    echo 'End test.<br />' . "\r\n";
     
  2. The the line end characters in the above should be either \n or \n\n
    I've tried editing the post but the forum software or something, complains that I'm submitting spam or inappropriate content.
    The code should be like this. Not that it solves the buffering problem!

    Code:
    $string_length = 32;
    echo 'Begin test with an ' . $string_length . ' character string...<br />' . "\n\n";
    
    // For 3 seconds, repeat the string.
    for ($i = 0; $i < 3; $i++) {
      $string = str_repeat('.', $string_length);
      echo $string . '<br />' . "\n";
      echo $i . '<br />' . "\n\n";
      ob_flush();
      flush();
      sleep(5);
    }
    
    echo 'End test.<br />' . "\n\n";
     
  3. Elshadriel

    Elshadriel Winhost Staff

    Hi John,

    Might be because you're using the wrong function. According to this, you probably want to use ob_end_flush() instead of ob_flush().
     
  4. Thank you for the suggestion. Unfortunately, it makes no difference. I've tried all ways I can think of to solve this including an ob_start paired with an ob_flush etc. Here is a simple php script to show the problem. It outputs all 3 lines instantaneously after 10 seconds when the script terminates. If anyone could save this as something.php and say if it works for them, that would be useful. Perhaps the problem is at my client end. I've tried Chrome and Firefox.

    Code:
    <?php
        header( 'Content-type: text');
        header('Cache-Control: no-cache');
    
        $time = date("H:i:s");
        echo  "Time: {$time} About to flush then wait 5 seconds\n";
        ob_end_flush();
        flush();
        sleep(5);
    
        $time = date("H:i:s");
        echo  "Time: {$time} About to flush then wait 5 seconds\n";
        ob_end_flush();
        flush();
        sleep(5);
    
        $time = date("H:i:s");
        echo  "Time: {$time} Finished\n";
        ob_end_flush();
        flush();
    ?>
    
     

Share This Page