What is a WebSocket
A WebSocket is a communication protocol that provides full-duplex, persistent communication over a single TCP connection. Unlike HTTP, which is request-response (client asks, server answers), a WebSocket lets both sides send messages independently at any time.
How it works
The connection starts as a normal HTTP request with an Upgrade: websocket header. If the server agrees, it responds with 101 Switching Protocols and the connection switches from HTTP to the WebSocket frame protocol. From that point, both sides can send text or binary frames without the overhead of HTTP headers.
Each frame has minimal overhead — 2-6 bytes versus hundreds of bytes for HTTP headers. Ping/pong frames serve as heartbeats to keep the connection alive through proxies and firewalls that would otherwise close idle connections.
Why it matters
WebSockets are the standard protocol for real-time web applications. Chat, multiplayer games, collaborative editing, live dashboards, and stock tickers all use WebSockets because the server can push data to the client the instant it becomes available — no polling, no long-held HTTP requests, no wasted round trips.
The tradeoff: WebSockets require stateful connection management, don't work through all proxies, and add complexity compared to simple HTTP request-response.
See How WebSockets Work for the upgrade handshake, frame types, and connection management.