Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server sent event vs web sockets?

I'm working on a web app that is accessible to users via multiple platforms from smartphones to desktops which needs to sometimes make a communication between two clients for example if I want my friend to join my network I'd send him a friend request but I want that request to be seen by my friend without him having to refresh the page.

In this scenario which would be a better choice? And also since I want this to work on as many platforms and browsers as possible which has more browser support? Is there a better option?

like image 527
user3053234 Avatar asked May 28 '14 14:05

user3053234


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.

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

WebSockets and SSEs: Differences The significant difference between both technologies is that WebSockets are bidirectional while SSEs are mono-directional. So, if you're eager to add server push functionality to your app, they're both viable options to explore.

Is SSE faster than WebSockets?

Transported over simple HTTP instead of a custom protocol. SSEs are most appreciated in the cases like updating statuses, push notifications, newsletters and news feeds because they support mono-directional communication. Compared to WebSockets, SSE is faster and more suitable to set up.

What is the best alternative for WebSocket?

WebTransport is a new specification offering an alternative to WebSockets. For applications that need low-latency, event-driven communication between endpoints, WebSockets has been the go-to choice, but WebTransport may change that.


2 Answers

Some things to keep in mind when making this choice.

  • Attempting to fetch content over a WebSocket connection is a poor design decision because WebSockets is a different protocol nested inside an HTTP connection and it can't leverage caching (neither the browsers nor CDNs).
  • Some older proxies won't pass on a Websocket connection unless its hidden within a secure connection while Server Sent Events remains an HTTP connection and won't suffer from this.
  • Neither WebSockets nor SSE are supported in the native Android browser until 4.4 (when they switched to using Chrome) - thus if you're considering a hybrid mobile app, you will need a fallback such as SocketIO since, as of this writing, 4.4 is only 20% of the market and hybrid apps use the native Android browser.
  • WebSockets is the most battery efficient protocol for mobile devices, since all other options require many HTTP connections and it is the repeated negotiating of the headers that will burden the cpu and drain the battery.

Another option may be notifications. All mobile devices now support notifications that can be targeted to an App and a number of browsers have as well. In all cases a connection already exists from the client to the messaging center (Apple, Google, Microsoft, etc) and all notifications are sent over this channel.

Here's a good overview of WebSockets vs. SSE: http://www.html5rocks.com/en/tutorials/eventsource/basics/

like image 61
JaysonRaymond Avatar answered Sep 22 '22 01:09

JaysonRaymond


  • Server Sent Events: A persistent connection server-2-client only, for sending text messages and that is implemented in all major browsers, but Internet Explorer. It can reconnect itself if connectivity is lost. http://caniuse.com/eventsource

  • WebSokets: A full duplex persistent connection capable of transmitting UTF8 text and binary data. http://caniuse.com/websockets

WebSocket is better, and the future.

like image 33
vtortola Avatar answered Sep 22 '22 01:09

vtortola