Networking & ProtocolsWebSocket & Real-time CommunicationEasy⏱️ ~2 min

WebSocket Fundamentals: Full Duplex Communication Over TCP

WebSocket is a stateful, full duplex protocol that operates over a single persistent TCP connection, established after an HTTP upgrade handshake. Unlike traditional HTTP where the client must repeatedly poll the server, WebSocket allows both client and server to send data frames (text or binary) at any time without request/response semantics or repetitive header overhead. This architectural shift transforms latency characteristics from poll interval plus network delay into just network delay plus queuing time, enabling sub 100 ms perceived latency when servers push events immediately. The protocol begins with a standard HTTP handshake where the client sends an Upgrade header requesting WebSocket protocol. Once the server accepts and responds with status 101 Switching Protocols, the connection is upgraded and remains open as a bidirectional stream. At this point, either party can send frames independently without waiting for the other. This persistent connection model makes the server accountable for maintaining connection state: tracking which clients are connected, what topics they subscribe to, managing heartbeats to detect liveness, and handling resumption after network interruptions. Compared to traditional polling, the efficiency gains are dramatic. Consider 100,000 clients needing timely updates: short polling every 5 seconds generates 20,000 requests per second. With just 2 KB header overhead per request and response, this produces approximately 40 MB/s or 3.5 TB/day of header only traffic, costing around $315/day at $0.09/GB egress rates. WebSocket with 30 second ping/pong frames of 20 bytes produces only 3,333 frames per second and 67 KB/s total (5.8 GB/day), a 600x reduction in overhead while delivering much lower latency.
💡 Key Takeaways
WebSocket establishes a single persistent TCP connection after HTTP upgrade, eliminating the need for repeated handshakes and reducing header overhead by orders of magnitude
Full duplex means both client and server can send data independently at any time, reducing latency from poll interval plus network delay to just network delay plus queuing
For 100,000 clients, polling at 5 second intervals costs approximately $315/day in header overhead alone versus WebSocket at 600x less bandwidth and cost
Persistent connections make servers stateful, requiring tracking of client identity, subscriptions, heartbeats, and resumption state across potentially millions of connections
Sub 100 ms perceived latency becomes achievable when servers push events immediately without waiting for client poll requests
📌 Examples
Meta Messenger uses a persistent lightweight messaging protocol over TLS on port 443 to minimize mobile radio wakeups and battery drain across billions of devices, multiplexing presence, typing indicators, and message signals over a single connection
Discord handles 5 million plus concurrent WebSocket connections across a sharded gateway tier, delivering multi million events per second during peak global moments with 99th percentile latencies under 200 ms for presence and typing indicators
← Back to WebSocket & Real-time Communication Overview