WebSocket is a communication protocol defined in RFC 6455 that enables full-duplex, bidirectional communication over a single TCP connection. Unlike HTTP's request-response model, WebSocket maintains a persistent connection where both client and server can push messages independently, making it ideal for real-time applications like chat systems, live dashboards, collaborative editing, and multiplayer gaming. The protocol starts with an HTTP upgrade handshake, transitions to a framing-based message exchange, and remains open until explicitly closed. One key consideration: WebSocket connections are stateful and long-lived, requiring careful design for scalability, authentication, reconnection, and resource management β especially when handling thousands of concurrent connections across distributed servers.
What This Cheat Sheet Covers
This topic spans 28 focused tables and 197 indexed concepts, 147 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Protocol Fundamentals
WebSocket is a thin framing protocol built on TCP, not HTTP β once the upgrade handshake completes, the semantics are entirely different. Understanding the framing model, connection lifecycle, and statefulness is the foundation before implementing anything else.
| Concept | Example | Description | |
|---|---|---|---|
wss://example.com/chatwss://example.com/updates | β’ Standard defining WebSocket communication β’ wss is the recommended TLS-secured scheme for production deployments. | ||
GET /chat HTTP/1.1Upgrade: websocketConnection: UpgradeSec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== | β’ HTTP upgrade request β’ client sends Sec-WebSocket-Key, server responds with 101 Switching Protocols. | ||
ws.send("Hello")ws.onmessage = (e) => {...} | Both client and server can send messages at any time without polling. | ||
Connection remains open until closed | β’ Eliminates HTTP overhead of repeated handshakes β’ reduces latency for real-time data. | ||
Client and server send simultaneously | Unlike half-duplex HTTP, both directions are independent and concurrent. | ||
Server tracks each client connection | β’ Connection state must be preserved β’ requires sticky sessions or state synchronization in multi-server setups. | ||
Binary frame with opcode, mask bit, payload length | β’ Each message is wrapped in a frame with metadata β’ supports text, binary, ping, pong, close. | ||
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= | β’ Server's response header β’ computed from client's key using SHA-1 hash to confirm handshake. | ||
Sub-100ms message delivery | β’ No HTTP overhead per message β’ ideal for time-sensitive applications like gaming or trading. |