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?

Related Terms

Try PubNub Today