Notes for using the PHP flush function and precautions for using the flush Function
Ob _ * series functions are used to operate the output buffer of PHP itself.
Therefore, ob_flush is used to refresh the PHP buffer.
But flush, strictly speaking, this is only useful when PHP is installed as an apache Module (handler or filter.
It is used to refresh the buffer zone of the WebServer (which can be considered to be specific to apache.
In the sapi of apache module, flush calls the flush member function pointer of sapi_module,
Indirectly calling apache api: ap_rflush refreshes apache's output buffer. Of course, some other apache modules are also mentioned in the manual,
May change the result of this action ..
Some Apache modules, such as mod_gzip, May output the cache by themselves, which will cause the results produced by the flush () function to be not immediately sent to the client browser.
Even the browser caches the received content before it is displayed. For example, the Netscape Browser caches content before the start of a line break or html Tag, and does not display the entire table until it receives the tag.
Some versions of Microsoft Internet Explorer
The page is displayed only after 256 bytes are received. Therefore, some extra spaces must be sent for these browsers to display the page content.
Therefore, the correct order of the two users is: first ob_flush, then flush,
Of course, other sapis do not call flush, but we recommend that you use it to ensure the portability of your code.
<?php// set_time_limit(0);header('Content-Type: text/event-stream');header('Cache-Control: no-cache');// ob_end_flush();// ini_set('output_buffering', 0);// ini_set('implicit_flush', 1);if (ob_get_level() == 0) ob_start();echo str_repeat(' ' ,4096);$long = 60;while($long > 0){$time = date('r');echo "data: The server time is: {$time}\n\n";ob_flush();flush();//break;sleep(1);$long --;}// var source=new EventSource("http://localhost:18000/sse.php");source.onmessage=function(event){console.info(event.data)};?>
If you want to support nginx + fpm + php, you need to add a response header.
Header ('x-Accel-Buffering: no ');
This eliminates both proxy_buffering and (if you have nginx> = 1.5.6), fastcgi_buffering. the fastcgi bit is crucial if you're using php-fpm. the header is also far more convenient to do on an as-needed basis.
Docs on X-Accel-Buffering http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffering;
<?php// set_time_limit(0);header('Content-Type: text/event-stream');header('Cache-Control: no-cache');header('X-Accel-Buffering: no');// ob_end_flush();// ini_set('output_buffering', 0);// ini_set('implicit_flush', 1);// if (ob_get_level() == 0) ob_start();// echo str_repeat(' ' ,4096);$long = 60;while($long > 0){$time = date('r');echo "data: The server time is: {$time}\n\n";ob_flush();flush();//break;sleep(1);$long --;}// var source=new EventSource("http://localhost:18000/sse.php");source.onmessage=function(event){console.info(event.data)};?>