This blog post compares two similar technologies: WebSockets and SSEs. We'll describe their similarities and differences. Next, we'll look at best use cases for both. Finally, we’ll have a look at the additional features offered by ScaleDrone's Enhanced WebSockets.
WebSockets and SSEs: the similarities
The last two blog posts covered WebSocket and SSE technology in detail. Let´s now have a look at the similarities between the two. For starters, both WebSockets and SSEs make use of HTTP connections, although in case of WebSockets a TCP handshake effectively upgrades from HTTP protocol to WebSocket protocol, allowing WebSocket applications to more easily fit into existing infrastructures. However, the main similarity between both is their functionality: both push data from the client to server, a process also known as “server push”. With this in mind, let´s now have a look at some differences between the two before we can talk about best use cases for both technologies.
Differences between SSEs and WebSockets
Before we get into the differences between both technologies, we should state they're not competing technologies, nor is one better than the other. The popularity of WebSockets over SSEs can be explained by the fac that WebSockets have received more attention (and appreciation) than SSEs, and it’s a fact that more browsers support it natively than they support SSEs.
The differences between both technologies are not that big, and if you want to do “server-push” only, both are good choices. Let´s have a look at their differences. By far the biggest difference between both technologies is that WebSockets are full-duplex, bidirectional communication between client and server, whereas SSEs are mono-directional.
SSEs come with a set of features that WebSockets lack by design, such as automatic reconnection, event IDs and sending arbitrary events. On the other hand, WebSockets are able to detect a dropped client connection, whereas SSEs first need to send a message before detecting the same issue. In terms of browser support, both Internet Explorer and Edge do not yet support SSEs, although polyfills that simulate SSE functionality are available to solve this issue.
Although WebSockets use an initial HTTP connection, this connection is updated after a TCP handshake after which data is sent through the WebSocket protocol. This is a more complex protocol than the SSE protocol. Because they’re bidirectional, WebSocktets require more development effort than SSEs, that only need to send an HTTP message with a specific header, whereas a WebSocket needs to establish and maintain a TCP socket communication, as well as a listener socket on the server side.
It´s good to know that SSE suffers from a limitation to the maximum number of open connections, which can be specially painful when opening various tabs as the limit is per browser is six. Only WebSockets can transmit both binary data and UTF-8, whereas SSE is limited to UTF-8.
While WebSockets are more complex and demanding than SSEs, a full-duplex TCP connection makes it useful for a wider range of application scenarios. SSE is a simpler and faster solution, but it isn't extensible: if your web application requirements were to change, it would need to be refactored using WebSocket. Although WebSocket technology presents more upfront work, it´s a more versatile and extensible framework, so a better option for complex applications that will add new features over time.
WS and SSE Best Use Cases
In the end, whether you should be using WebSockets or SSEs depends on your use case. As stated before, SSEs cannot provide bidirectional client-server communication as opposed to WebSockets. Use cases that require such communication are real-time multi-player games and messaging and chat apps. When there´s no need for sending data from a client, SSEs might be a better option than WebSockets. Examples of such use cases are status updates, news feeds and other automated data push mechanisms.
How Scaledrone uses WebSocket technology
Scaledrone offer a real-time messaging service and platform that can be used for sending live updates, create chatrooms and collaborative tools. Scaledrone makes use of Enhanced WebSockets that offer additional features. These are used when possible. When needed, it falls back to technologies such as XHR streaming, JSONP polling and XHR polling. Use cases for which they´re used are chat, notifications and real-time updates, for the creation of live maps, real-time dashboards and much more.
In this blog post we compared WebSockets and SSEs. We discussed the differences and similarities between the two technologies. While both can be used for “server push”, there are many subtle differences between the two. Because WebSockets have received more attention from developers and bloggers, SSEs are lesser-known but no less potent. We looked at best use cases for both and concluded that the use case should define your final choice if “server push” is what you´re after. Finally, we described how Scaledrone uses WebSocket technology to drive the real-time web.