2

At home, I'm trying to create a SSE Endpoint to use over website, true. Then,I have the code fragment that's keeps request open, obviously (while( true )) ... So then, I echo something and then use ob_flush() and flush() inside.

But, at home it's works properly, at remote host, no.

Localhost definition. System: Linux localhost 3.6.10-2.fc17.x86_64 #1 SMP Tue Dec 11 18:07:34 UTC 2012 x86_64

Server API: Apache 2.0 Handler

setting           local value        master value
output_buffering       0                 0
output_handler     no value           no value

And in server ... ( remote ) Linux web521.xxx.net 3.2.2 #1 SMP Wed Feb 1 09:54:51 BRST 2012 x86_64

Server API FPM/FastCGI

setting           local value        master value
output_buffering    no value    no value
output_handler  no value no value

Code fragment:

<?php

    while( true ) {
        // ...

        echo '...';
        ob_flush();
        flush();

        usleep( 50000 );

        // ...
    }

?>
Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
Haro Ken
  • 41
  • 1
  • 5
  • Added ... echo '...' is event source data format, the problem is .. at remote host, the data not get flushed while scripts die. – Haro Ken Jan 06 '13 at 02:38
  • 1
    Interesting. Perhaps the FastCGI bits aren't passing along the data? – Charles Jan 06 '13 at 02:40
  • Probably ... some idea of how to fix ? – Haro Ken Jan 06 '13 at 02:41
  • @HaroKen I've posted a similar script, which may help: http://stackoverflow.com/questions/14120084/output-loading-message-in-php/14120513#14120513 You can try it and see if it works – Xiao Jia Jan 06 '13 at 09:39
  • 2
    BTW: it's safer to use `while(!connection_aborted())` instead of `while(true)`. – Kornel Sep 01 '13 at 18:30

1 Answers1

0

I had this same problem, and found the answer here: https://stackoverflow.com/a/12301929/1314762

Try using ob_end_flush() in addition to flush(). I have no good answer as to why, but this worked for me when ob_flush() did not.

As an example (and I'm borrowing porneL's nice comment about using connection_aborted() instead of while(true)):

while( !connection_aborted() ) {
    // ...

    echo '...';
    ob_end_flush();
    flush();

    usleep( 50000 );

    // ...
}
Community
  • 1
  • 1
Don McCurdy
  • 10,975
  • 2
  • 37
  • 75