termEngineering & architecture

WebSocket

A protocol for maintaining a persistent connection over which a client and server can independently exchange messages in both directions.

Download Markdown (.md)

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]

A persistent full-duplex channel established through HTTP After the opening handshake, either peer can send framed messages
ClientServer
  1. 01Client ServerHTTP upgradeOrigin, authentication, and protocol options
  2. 02Server ClientSwitch protocolsAccept the persistent connection
  3. 03Client ServerApplication messageTyped text or binary frame
  4. 04Server ClientIndependent messagePush without waiting for a new request
  5. 05Client ServerPing, recovery, reconnectDetect failure and restore subscriptions or missed state

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

  1. The client sends an HTTP upgrade handshake and the server validates origin, authentication, and protocol options.
  2. After a successful handshake, both peers exchange text or binary message frames on the open connection.
  3. The application defines message types, identifiers, permissions, ordering expectations, and error behavior.
  4. Ping, pong, timeouts, and monitoring detect unusable connections and release resources.
  5. Clients reconnect with bounded backoff and restore subscriptions or missed state through an explicit recovery strategy.

05A practical example

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

  1. The WebSocket Protocol (RFC 6455)RFC Editor · Internet StandardThe WebSocket Protocol (RFC 6455)
  2. WebSocket APIMDN Web Docs · Official technical documentationWebSocket API
View saved topics

Start typing to search titles, definitions, categories, and frameworks.