JavaScript: EventSource is Long Polling

UPDATE: With some serious modification to the server end point, Luke Morton was able to achieve a single open event that fires message events as responses are received via a “push” from the server: https://gist.github.com/1002722. By modifying the original example’s PHP end point, Luke’s approach was to omit the closing PHP tag (ensures that no return code is sent to the client), buffer and flush the output and wrap the whole program in a while loop that sleeps for one second to simulate delays in response activity.

Now that we’ve cleared that up, here is the original post:

Since Mozilla’s announcement this past weekend ( “Aurora 6 Is Here” ) and my own subsequent recap of previously published materials, there has been a noticeable uptick in attention towards the EventSource API, including a new polyfill for the feature.

As always, positive attention towards new JavaScript APIs is nothing but awesome — however — there seems to be a misunderstanding in the behaviour of an EventSource instance. Specifically, the idea that EventSource (in it’s current implementations) is capable of server-push, is incorrect. To demonstrate this fact, I’ve prepared a set of code examples, which can be downloaded here. Follow along with the comments in the code examples, to learn how the current implementations of the EventSource API actually work.

This is a quick look at a client that creates a single EventSource instance. The comments within are a paraphrasing of the spec production and the observed, testable behaviour.

Here we have a basic EventSource end point located on the server. The comments within will detail the behaviour that is created when a response body is not provided and when it is.

And a simple html file to run the whole thing on your localhost:

This entry was posted by Rick Waldron (@rwaldron) on June 01, 2011 in Chrome, EventSource, Firefox, JavaScript and Opera.

Comments

Author

This entry was posted by Rick Waldron (@rwaldron) on June 01, 2011 in Chrome, EventSource, Firefox, JavaScript and Opera.

Recent posts from this author

Related on the Bocoup Blog

Advertisement

Twitter

Google+