WebSocket is a communication protocol that provides full-duplex, bidirectional communication between a client and server over a single, persistent TCP connection. In contrast to HTTP's request-response model where the client must ask for updates, WebSocket lets the server push data to clients instantly whenever something changes.
How It Works
A WebSocket connection starts as a regular HTTP request with an "Upgrade" header. If the server supports WebSocket, it responds with a 101 status code, and the connection switches from HTTP to the WebSocket protocol. From this point, both sides can send messages at any time without the overhead of HTTP headers on each message.
Messages are lightweight frames, often just a few bytes of overhead compared to hundreds of bytes for HTTP headers. Either side can send text or binary data, and the connection stays open until explicitly closed. Libraries like Socket.IO add features on top of raw WebSockets, including automatic reconnection, room-based broadcasting, and fallback transports for environments where WebSocket is blocked.
Why It Matters
Real-time applications cannot wait for users to refresh or poll for updates. Chat applications need instant message delivery. Collaborative editors need to synchronize keystrokes across users in real time. Trading platforms need sub-second price updates. Live sports scores, multiplayer games, and IoT dashboards all require the server to push data the moment it changes.
HTTP polling (asking "any updates?" every few seconds) wastes bandwidth and introduces latency. Long polling improves this but still reopens connections repeatedly. WebSocket solves this with a single persistent connection that delivers updates in 1-2 milliseconds.
In Practice
A collaborative document editor like Notion uses WebSocket to synchronize edits between users. When Alice types a paragraph, her client sends the change through the WebSocket connection. The server broadcasts the update to all connected users viewing that document. Bob's screen updates within 50 milliseconds, without any page refresh or polling interval.
Pro Tips
Always implement reconnection logic because connections drop on network changes. Use heartbeat pings to detect stale connections. Consider Server-Sent Events (SSE) if you only need server-to-client streaming without bidirectional communication. For scalability, use Redis Pub/Sub or similar systems to broadcast across multiple server instances.