HTML5 Server Event: EventSource and WebSocket Wrapped

Is the HTML5 server initiated API (SSE) just limited to event-based APIs on top of HTML5 WebSockets?

It seems to me that EventSource is just a WebSocket , which:

  • Impossible .send() data
  • Uses the text/event-stream format
  • Creates dynamically called (server-defined) events instead of onmessage

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

I assume that the async event model will work beautifully when paired with Node, but will not see many use cases for this in my ASP.NET world.

+3
javascript html5 websocket server-sent-events
source share
2 answers

Events sent by the server are useful in applications that only require a load on the server, while web sockets are good for applications that need fast communication in both directions.

Examples when Server Sent Events are a good solution:

  • Stock price changes
  • News feeds

Events sent by the server perform several more functions that are not built into web sockets, such as automatic reconnection and event IDs.

Events sent by the server also have wider web browser support to date, with support in Safari (only old web socket projects are supported) and Opera (by default, web sockets are disabled and an older draft is used).

Learn more about events sent by the server on streaming updates with events sent by the server .

+7
source share

In addition to what Jonas said, the protocols are completely different.

  • The WebSocket Protocol (RFC 6455) starts as an HTTP connection, and then handshakes are used to update the connection to the new protocol. This is a binary protocol that uses framing, message types, etc.

  • Server-Sent Events is a long HTTP request that remains open. The server sends messages in plain text format ( UTF-8 encoding), limited to \n\n . The message has fields event (type of event), data , id and may contain comments.

One of the main differences is the security model. Using WebSockets, any connection is used by default. Connection rejection should be done on the server side based on the Origin header.

SSE, on the other hand, is closer to HTTP and uses a policy of the same origin. By default, you can send requests to only one host and port. In the future, it will be possible to use CORS to make gateway SSE requests. To date, browsers have not yet implemented this.

The two protocols use different approaches because they solve different problems.

+5
source share

Source: https://habr.com/ru/post/650964/


All Articles