Systems and protocols · Intermediate

WebSocket: a continuing bidirectional channel

Distinguish opening a channel from message delivery, authorization and reconnect recovery.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. From exchanges to a channel
  2. Frames and application messages
  3. Disconnections and slow consumers
  4. Channel access and operation access
  5. Ask Logeia about this articleCheck your understanding

From exchanges to a channel

WebSocket provides a continuing channel on which client and server can both send messages after connection establishment. This is useful for chat, collaborative state and server-originated notifications. It is different from repeatedly treating each update as the final response to a new independent request.

The original protocol begins with an HTTP opening handshake and then uses WebSocket frames. A successful opening handshake establishes the channel; it does not establish that every future message is processed or that reconnecting later restores application state.

Frames and application messages

WebSocket supports text and binary messages, and a message can be fragmented across frames. The application should define its event format explicitly, including type, identifiers and payload validation. A JSON payload is still untrusted input that must be checked for structure and allowed operations.

Numbering editing events can help compare the last applied event after a reconnect. Merely observing an open socket cannot reveal which edit was committed. Transport framing and application-level acknowledgment solve different problems and should not be conflated.

Disconnections and slow consumers

Persistent connections still fail. Decide whether reconnecting clients receive a full state snapshot or resume from an event position. If messages are resent, the application needs a way to avoid applying the same event twice. Ordering within one connection does not automatically extend across disconnected sessions.

A sender faster than its receiver can accumulate an unbounded queue. Set queue limits and consider coalescing stale updates or disconnecting consumers that cannot catch up. Capacity depends on pending data per connection as well as the number of connections.

Channel access and operation access

Using an encrypted wss connection does not implement operation-level authorization. A connected user must still be permitted to subscribe to a room or modify a document. For browser clients, handle the Origin context appropriately rather than treating it as interchangeable with user identity.

Ping and Pong help with channel liveness, not proof that business operations are succeeding. Observe connection health, authentication expiry and message-processing failures separately. The channel can be alive while an application worker is failing to apply every update.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 10

Choose a learning path