EventSource landed in Aurora 6. It is a new and simplified way to open long-lived connections to a server, and let the browser create events as the server streams messages to the client. It is also available in Chrome and Opera and there are fallback solutions for other browsers.
Creating a wall/feed for a social app…
…in a few lines of code (full project available on Github).
The messages
The server will send two kinds of messages:
● simple messages, starting on a new line prefixed with “data:”
● messages with specific event names, similar to simple messages but with “event: <anEventName>” on the previous line
In this case, simple messages are treated as users’ statuses and specific events will be inserted in the timeline with specific colors, although they could appear in different places on the page. The message data will be sent as JSON, although it could be flat text strings.
The server
The server will be a dummy .php script that reads sample statuses from a text files and stream them, one at a time, to the client, using appropriate headers.
The Client
The client will create an event source and register event handlers for each specific event name, as well as an onmessage
handler for simple messages.
The missing pieces of the code are available on Github.
Fallbacks
Here is a short list of polyfills/fallbacks available for other browsers:
● Remy Sharp’s polyfill
● Yaffle’s polyfill
● Rick Waldron’s jquery plugin
Have you got examples of EventSource based Web app to share?
About louisremi
Developer Relations Team, long time jQuery contributor and Open Web enthusiast. @louis_remi
23 comments