What is WebSocket?
WebSocket is a transport protocol defined by a persistent, bidirectional communication channel between server and client that takes place over a single TCP socket connection.
It was designed to overcome the limitations of HTTP's basic request/response mechanism (i.e. constant opening and closing of connections and header overhead), which is not suitable for real time applications that rely on sustained user interaction or continuously streamed updates.
Fully HTML5-compliant and built-in to all major web browsers, WebSocket enables full-duplex communication between server and client for use in applications such as chat, multiplayer gaming, multi-user collaboration, and live-streamed charts or scoreboards (e.g. sports matches in progress).
How WebSocket Works
To initiate a WebSocket connection, the client and server must negotiate an Upgrade handshake from HTTP, and from that point onwards, asynchronous WebSocket rules apply. Specifically, bulky HTTP headers are replaced with message frames only a few bytes in size, and both the server and client can simultaneously send new data without the other asking for it.
WebSocket is similar to Server-Sent Events (SSE), however Server-Sent Events is applicable only in unidirectional communication (i.e. server to client). Meanwhile, HTTP/2 Server Push is also related to WebSocket in concept, but Server Push is only able to push data from the server to the browser cache, and does not allow data to be pushed inside the application layer, as in WebSocket.
What is SockJS?
Benefits of SockJS
- Enhances third-party server implementation as it addresses various behavior patterns for most server implementations.
- All information is found in its URL, which means there is no need to use cookie-based sticky sessions.
- It truly supports cross-domain connectivity where it has the capacity to provide streaming transports through seamless connection instead of repeated HTTP connection requests for polling transports.