WebSocket
A protocol for maintaining a persistent connection over which a client and server can independently exchange messages in both directions.
Download Markdown (.md)Soroush’s Product Wiki · https://wiki.soroush.uk/topic/websocket/
01What it means
WebSocket is a stateful, full-duplex communication protocol that begins with an HTTP opening handshake and then exchanges framed messages over a persistent connection. Unlike ordinary HTTP request-response interaction, either peer can send data after the connection is established.[1][2]
WebSocket defines transport framing—not application schemas, durable delivery, replay, authorization updates, conflict resolution, or reconnection semantics.
02Why it matters
WebSocket can deliver frequent updates with lower per-message overhead and latency than repeated polling. It supports collaborative editing, live dashboards, chat, and games, while requiring explicit connection lifecycle, capacity, authorization, and recovery design.
03When to use it
Use WebSocket when a product needs frequent low-latency messages in both directions or server-initiated updates over a session. Ordinary HTTP, server-sent events, long polling, or webhooks may be simpler for infrequent, one-directional, or server-to-server updates.
04How it works
- The client sends an HTTP upgrade handshake and the server validates origin, authentication, and protocol options.
- After a successful handshake, both peers exchange text or binary message frames on the open connection.
- The application defines message types, identifiers, permissions, ordering expectations, and error behavior.
- Ping, pong, timeouts, and monitoring detect unusable connections and release resources.
- Clients reconnect with bounded backoff and restore subscriptions or missed state through an explicit recovery strategy.
05A practical example
A collaborative planning board opens a WebSocket after initial HTTP page load. The server pushes card moves to connected participants and clients send edits through the same connection; sequence identifiers reveal gaps so a reconnecting client can fetch a fresh snapshot.
06Where it breaks
Persistent connections consume resources and complicate horizontal scaling, deployments, proxies, mobile network changes, and observability. The protocol does not define application message schemas, authorization updates, durable delivery, replay, or conflict resolution.
Common mistakes
- Choosing WebSocket for occasional updates that ordinary HTTP can handle more simply.
- Assuming an open connection guarantees delivery, ordering across reconnects, or durable history.
- Authenticating only at connection time and not authorizing individual subscriptions or actions.
- Reconnecting every client immediately after an outage and creating a connection storm.
07Sources
- The WebSocket Protocol (RFC 6455)RFC Editor · Internet StandardThe WebSocket Protocol (RFC 6455)
- WebSocket APIMDN Web Docs · Official technical documentationWebSocket API