# WebSocket

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

- Type: term
- Domain: Engineering & architecture
- Level: Intermediate
- Last reviewed: 2026-09-23
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/websocket/>)

## What 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.

## Why 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.

## When 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.

## How 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.

## A 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.

## Where 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.

## Visualization

[View the visualization on the website](<https://wiki.soroush.uk/topic/websocket/#visualization-websocket>)

## Sources

1. [The WebSocket Protocol \(RFC 6455\)](<https://www.rfc-editor.org/rfc/rfc6455>) — RFC Editor · Internet Standard
2. [WebSocket API](<https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API>) — MDN Web Docs · Official technical documentation

## Related topics

- [Client and Server](<https://wiki.soroush.uk/topic/client-and-server/>)
- [Request and Response](<https://wiki.soroush.uk/topic/request-and-response/>)
- [Event-Driven Architecture](<https://wiki.soroush.uk/topic/event-driven-architecture/>)
