WebSockets
Standardized by IETF and W3C
WebSockets is a communication protocol that establishes a persistent, full-duplex connection over a single TCP connection, allowing a client and server to send messages to each other at any time without the repeated request-response…
Definition
WebSockets is a communication protocol that establishes a persistent, full-duplex connection over a single TCP connection, allowing a client and server to send messages to each other at any time without the repeated request-response overhead of standard HTTP. It is commonly used for real-time web applications such as chat, live notifications, and collaborative tools where the server needs to push data to the client without the client asking first.
Overview
Standard HTTP is built around a request-response cycle, where a client asks for something and the server responds, which makes server-initiated updates awkward and historically forced workarounds like polling or long-polling to simulate real-time behavior. WebSockets was designed to solve this directly by defining a protocol that upgrades an initial HTTP connection into a persistent, bidirectional channel, letting either side send data at any point without initiating a new request each time. Mechanically, a WebSocket connection begins as a normal HTTP request containing an 'Upgrade' header, which, if the server agrees, causes the connection to switch protocols rather than closing after a response as regular HTTP would. Once upgraded, both client and server can send discrete messages, text or binary, over the same underlying TCP connection at any time, without wrapping each one in HTTP headers, which significantly reduces overhead for frequent, small messages compared to repeated HTTP requests. The connection remains open until either side closes it, and a lightweight framing protocol distinguishes individual messages within that ongoing stream. WebSockets differs from HTTP polling and long-polling by eliminating repeated connection setup and the request-driven flow entirely, and differs from Server-Sent Events by supporting bidirectional communication rather than a server-to-client-only stream. It also differs from WebRTC, which is built for peer-to-peer media and data channels typically without a central server relay, whereas WebSockets connections are inherently client-server. Choosing among these depends on whether communication needs to flow in both directions and whether a central server should mediate every message. In practice, WebSockets power live chat applications, collaborative document editing, real-time dashboards, multiplayer game state synchronization, and financial ticker feeds, anywhere a server needs to push frequent updates to connected clients with low latency. Frameworks and libraries like Socket.IO build additional features such as automatic reconnection and room-based broadcasting on top of the raw WebSocket protocol. The main trade-offs are operational: maintaining many long-lived open connections requires different server infrastructure and scaling strategies than stateless HTTP request handling, since a server must track connection state for every connected client rather than treating each request independently. WebSocket traffic can also be blocked or mishandled by some older proxies and firewalls configured only for standard HTTP. For applications that only need occasional server-to-client updates rather than true bidirectional real-time messaging, simpler alternatives like Server-Sent Events or periodic polling may be a better operational fit and easier to deploy behind existing infrastructure without special handling.
Specification
- Establishes a persistent, full-duplex connection over one TCP connection
- Begins as an HTTP request that upgrades to the WebSocket protocol
- Allows both client and server to send messages at any time
- Reduces overhead compared to repeated HTTP request-response cycles
- Supports both text and binary message framing
- Differs from Server-Sent Events by allowing bidirectional communication
- Requires stateful server infrastructure to track open connections