Event Source & # 8594; The server returns a stream of events in bulk, and then returns to the piece

I have a php script that imports big data from csv files with validations.
To do this, I need to show the progress to the user. For this, I used Event Streaming.
When I repeat something, I want it to be transmitted to the client one by one, and the server sent all the output in bulk.
I already played with ob_start (), ob_implicit_flush () and ob_flush (), but they did not work.
My script is working fine on another server. The following are server configurations:

Server configuration on which the code does not respond as desired, i.e.

  OS: Linux
 PHP Version 5.4.36-0 + deb7u3
 Server API: CGI / FastCGI 
 Memory_limit: 128M
 output_buffering: no value

As I said, the code works correctly on another server that has almost the same configuration, i.e.

  OS: Linux
 PHP Version 5.4.37
 Server API: CGI / FastCGI 
 Memory_limit: 256MB
 output_buffering: no value

The following is sample code for sending an event:

<?php header("Content-Type: text/event-stream"); header("Cache-Control: no-cache"); header("Access-Control-Allow-Origin: *"); $lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0); if ($lastEventId == 0) { $lastEventId = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0); } echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE echo "retry: 2000\n"; // event-stream $i = $lastEventId; while ($i <= 100) { if($i==100){ echo "data: stop\n"; ob_flush(); flush(); break; } else { echo "id: " . $i . "\n"; echo "data: " . $i . ";\n\n"; ob_flush(); flush(); sleep(1); } $i++; } ?> 

Below is my client page where I need an answer:

 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>EventSource example</title> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <script src="../jquery/eventsource.js"></script> <script> var es = new EventSource("events.php"); var listener = function(event) { console.log(event.data); var type = event.type; if (event.data == 'stop') { es.close(); } else { var div = document.createElement("div"); div.appendChild(document.createTextNode(type + ": " + (type === "message" ? event.data : es.url))); document.body.appendChild(div); } }; var errlistener = function(event) { es.close(); } es.addEventListener("open", listener); es.addEventListener("message", listener); es.addEventListener("error", errlistener); </script> </head> <body> </body> </html> 
+7
javascript linux html5 php event-stream
source share
3 answers

The best way to return data to the browser is to use web sockets so that the client can open the socket for your file reader, after which you can easily transfer the data to the browser.

Then, as soon as it ends, you can close the socket.

good tutorial for web sockets http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html

using this method, you can then, if you want to check the verification, so that the server does not just send pieces, it sends pieces by javascript request

So your client can say that I need piece 5, and your server implements something like

 $requestedChunk = 5; // this would be set by the javascript sending the request $chunkSize = 256; // this would be your chunk size; $readPossition = $requestedChunk * $chunkSize; 
+6
source share

I had a similar problem. Thread streams worked as expected (return chunks) on the server using the Apache 2.0 handler, but not on the server using FastCGI (returning it in bulk). I assumed that something in FastCGI is the culprit and therefore tried to solve the problem by switching to CGI. The event flow now works as expected.

Is the CGI or FastCGI server API used as CGI / FastCGI, so I assume that the server on which it works is CGI for you, and the server on which it is not running works for you FastCGI. Try changing the down server to CGI.

As for why it does not work in FastCGI, I'm not quite sure, but if this is not required and CGI is not possible, then this solution should work.

0
source share

Many things can prevent an alternating response, such as, but not limited to:

  • Proxy server or any other buffering mechanism on the web server
  • When "Output Buffering" is "on" in php.ini (you must explicitly disable it)
  • When gzip is enabled on a web server

You must check them first.

0
source share

All Articles