Search code examples
javascriptpythonflaskserver-sent-events

Does server sent events is "Busy Wait"?


I'm building a web site using flask and I wish to do a Push to the client. I've followed real-time-events-python and I was able to create the website.

One thing that I've noticed is that when accessing the Javascript console, there is a GET every 500ms, so I'm wondering if the EventSource of javascript actually sends a GET to the server periodically to see if there are any updates, causing it to be a Busy Wait.

For information, I'm using Flask (python framework) for developing the website and chrome to access it.

Server Sent Events specification


Solution

  • According to the link you provided, yes, the browser sends GETs as an implementation for server-sent events:

    The actual protocol for Server-Sent Events is very simple. The client will open a standard connection to the server and make a GET request. It expects the server to hold open the socket and send new events by prefixing them with data: and terminating with two newline characters.

    So, on the server side, the connection is supposed to remain open, while data is still being streamed through it. Keep in mind that Server-Sent Events allows for automatic reconnection, so if you are experiencing a lot of reconnects (which I imagine is what all those gets are, unless your client-side code is not written correctly), you should check to make sure that your server side is not closing the connection, which causes the browser to reopen the connection.

    As for the "busy wait", if I'm understanding you correctly, you don't need to worry about this. This is handled by the browser, so your code doesn't block while waiting for something.