I have seen tutorials on the net where the author sets up SSE/EventSource
and in the server PHP script echos out some data and then calls ob_flush()
and flush()
to send the data back to the client browser. And this works fine and new data gets pushed to the client every 3 seconds.
But I also read a comment from somebody saying that this is the wrong way to use SSE. This person said that the whole point of SSE is that the server is supposed to maintain the connection and the reason the new data gets pushed to the client using the above method is because the EventSource realises the server disconnected. This person said the server should be using a loop and pushing the data out every few seconds by using sleep.
So, is this the case? Should SSE be implemented by having a loop on the server and pushing data, using sleep()
to pace the pushing?
sleep();
seams reasonable (that's how the MDN article does it). – Šime Vidas