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). Because a WebSocket connection is held open for the duration of a session, messages can flow back and forth between participating endpoints with little overhead and low latency. This eliminates the need for client-server polling and is especially useful for any JavaScript-based application that processes data frequently, asynchronously, and in real time.

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 websocket?

What is SockJS?

SockJS is a JavaScript library that provides WebSocket-like connectivity, delivering low-latency communication between web browsers and servers, and even applications that do not utilize WebSocket programming. Under the hood, SockJS uses native WebSockets first, and on failure, falls back to a variety of browser-specific transport protocols, and delivers them through WebSocket-like abstractions.

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.

Related Terms

MORE FROM PUBNUB
Overview Of Real-time Streaming Protocols

Overview Of Real-time Streaming Protocols

Making sense of the crowded, complex communication protocol ecosystem.
WebSockets vs REST: Understanding the Difference

WebSockets vs REST: Understanding the Difference

This blog post looks at WebSockets vs REST, the differences in performance, use cases, and how to take WebSockets to the next level.