Search code examples
javascripthtmlwebsocketserver-sent-events

HTML5 Server-Side Event: EventSource vs. wrapped WebSocket


Is the HTML5 Server-Sent Events (SSE) API just a restricted, event-based API on top of HTML5 WebSockets?

It seems to me that an EventSource is just a WebSocket that:

  1. Cannot .send() data
  2. Uses the text/event-stream format
  3. Fires dynamically-named (server-defined) events instead of onmessage

The idea of the web server pushing events down to client devices is quite intriguing. Does this API have any traction?

I imagine the async event model would work beautiful when couple with Node, but not seeing a lot of use cases for this in my ASP.NET world.


Solution

  • Server Sent Events is useful in applications that only needs server push while Web Sockets are good for applications that needs fast communications in both directions.

    Examples where Server Sent Events are a good solution are:

    • Stock Value changes
    • News feeds

    Server Sent Events do some more things that is not built-in in Web Sockets, such as automatic reconnection and eventIDs.

    Server Sent events also has broader web browser support as of today, with support in Safari (only support older drafts of Web Sockets) and Opera (has Web Sockets disabled by default, and uses an older draft).

    Read more about Server Sent Events on Stream Updates with Server-Sent Events.