2
votes

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?

1
Also, check out this question: stackoverflow.com/questions/9565290/…Šime Vidas
"On the server-side, the script (...) sends messages in the following form, with the text/event-stream MIME type." What has that got to do with my question? And that other link you posted is also irrelevant to my question which is what is the recommended way of implementing SSE, .ie should we always use an infinite loop on the server?sonicboom
That other question is "How to fire SSE events?", and you're asking "How to implement SSE?". It may not be the same exact question, but I don't think that other question is irrelevant.Šime Vidas
One of the great things about stackoverflow is helping each other out so that we don't have to go digging through lengthy jargon filled standards documents that may or may provide an answer. FWIW I went and looked through the standard in your link and didn't find an answer.sonicboom
From what I understand, the server is not supposed to close the connection, so the tutorial you linked above is doing it wrong. I'm not a PHP programmer, but the solution with a loop and sleep(); seams reasonable (that's how the MDN article does it).Šime Vidas

1 Answers

2
votes

I don't know PHP well enough to say if sleep() the only way, but yes the point on the server side is to keep the connection open so that new data can be sent to the client at any time without opening a new connection.

There are a couple of wrinkles to this. Since SSE is designed to work with traditional shared web hosting, there is expected to be a timeout on the server side at some point (30 seconds or 60 seconds, in PHP depending on max_execution_time and/or set_time_limit(). This leads to the second wrinkle, because the server is expected disconnect periodically the browser is expected to automatically re-establish the connection, which explains the behaviour seen in the blog post you mention - the browser automatically re-connects so it appears as if SSE is working.

I think with sleep() you lose some of the real time aspect as if you're only sending data every second anyway you might as well use AJAX polling, but I know of no better way in PHP than the loop/sleep approach. Also, if you're using PHP for SSE, remember that the session object is single threaded so if you hold it open in your SSE page it will block any other scripts (eg. AJAX requests) which require access.