Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downside of using Server-Sent events for bidirectional client-server communication (instead of WebSockets)

Recently I've found out of Server-Sent events as a much simpler alternative to WebSockets for doing push from the server. Most places that compare them (like here, here and here) say that if you don't need full duplex communications between client and server, then WebSockets is overkill and SSE are good enough.

My question is what would be the downside of using SSE when you do need bidirectional communications (like a chat for example), using regular ajax requests for sending messages from the client and the server stream for receiving them? Considering that I have to do little to no configuration on the server side to use SSE, it seems to be a much more appealing option.

like image 894
Facundo Olano Avatar asked Nov 07 '12 21:11

Facundo Olano


People also ask

When to use server-sent events vs WebSockets?

SSE is best used when it's not necessary to send data from client to server. For example, in status updates and push notification applications, the data flow is from the server to the client only. This is what SSE is designed for, so WebSocket would be overkill. It's always wise to use the best tool for the job.

Why use server-sent events over WebSockets?

WebSockets generally do not use 'XMLHttpRequest', and as such, headers are not sent every-time we need to get more information from the server. This, in turn, reduces the expensive data loads being sent to the server. WebSocket connections can both send and receive data from the browser.

What is the difference between server-sent events SSEs and WebSockets in html5?

Obviously, the major difference between WebSockets and Server-Sent Events is that WebSockets are bidirectional (allowing communication between the client and the server) while SSEs are mono-directional (only allowing the client to receive data from the server).

Are WebSockets bidirectional?

BIDIRECTIONAL. Whereas HTTP relies on a client request to receive a response from the server for every exchange, WebSockets allow for full-duplex bidirectional communication. This enables the server to send real-time updates asynchronously, without requiring the client to submit a request each time.

What is the difference between WebSockets and Server Sent Events?

Differences Obviously, the major difference between WebSockets and Server-Sent Events is that WebSockets are bidirectional (allowing communication between the client and the server) while SSEs are mono-directional (only allowing the client to receive data from the server).

What is WebSocket communication?

WebSocket communication is a separate communication protocol from HTTP, introducing new problems in the network layer, as I should soon find out. But there is an alternative: Server Sent Events (SSE).

Why should I use Server Sent Events?

Server Sent Events win 3:0. The better performance is noticable in all browsers, especially in Safari though, which seems to have a less-than-ideal WebSocket implementation. This was actually super simple to implement, it took much longer to write this blog post than to implement a working solution using Server Sent Events.

What are Server-Sent Events?

As the name suggest, server-sent events are a push technology that allows the client to receive data automatically from the server through an HTTP connection. In this case, after an HTTP connection has been established between the server and the client, the server can send automatic updates.


1 Answers

SSE Advantages over WebSockets:

  • No special web server or web proxy changes required.
  • Define custom events (otherwise, client API is basically the same)
  • Easier integration of existing authentication mechanisms (OAuth, OpenID, etc)

SSE Disadvantages compared to WebSockets:

  • Unidirectional communication channel (server to client). Client to server requires a separate channel.
  • Browser support is more limited (no native IE support whereas WebSockets is supported in IE 10): WebSockets, SSE
  • Relies on client to verify origin (possibly more vulnerable to XSS attacks than WebSockets)
  • No native support for binary types (WebSockets supports raw frames using ArrayBuffers and Blobs).
  • Requires a full fledged web server even if the SSE endpoint is not serving static web content (a standalone WebSocket server can be fairly simple)
  • SSE with AJAX for bi-directional communication will have MUCH higher round-trip latency and higher client->server bandwidth than using a WebSocket connection. This is due to the overhead of connection setup for every client->server AJAX request. Also, server->client latency can have spikes with SSE since in many configurations the long-held connection will eventually be closed (often every 30 seconds) and need to be re-opened causing a temporary spike in server->client latency as well.

References:

  • http://www.html5rocks.com/en/tutorials/eventsource/basics/
  • https://developer.mozilla.org/en-US/docs/Server-sent_events
  • https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events
  • http://dev.w3.org/html5/eventsource/
like image 156
kanaka Avatar answered Jan 03 '23 04:01

kanaka