Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

WebSocket client

This page is a practical guide for building a WebSocket client.

Minimal flow

Pseudo-code (server-side, using Authorization header):

token = "AT_..."
ws = connect("wss://ws.fastpubsub.com/",
             headers={"Authorization": "Bearer " + token})

subscribe(tenant="prod", channel="orders.eu.#")

on_message(msg):
  handle(msg)

publish(tenant="prod", channel="orders.eu.created", payload="...")

Browser example (using Sec-WebSocket-Protocol subprotocol):

const token = "AT_...";
const ws = new WebSocket("wss://ws.fastpubsub.com/", [
  "llps.v1",
  "at." + token
]);

ws.onopen = () => {
  ws.send(JSON.stringify({
    action: "subscribe",
    tenant: "prod",
    channel: "orders.eu.#"
  }));
};

ws.onmessage = (e) => { /* handle message */ };

Where to put the token:

MethodWhen to use
Authorization: Bearer AT_... headerServer-side clients (Node.js, Python, Rust, etc.)
Sec-WebSocket-Protocol: llps.v1, at.AT_...Browser clients (the browser WebSocket API cannot set custom headers)

Priority: Authorization header is checked first; Sec-WebSocket-Protocol is used as a fallback.

See WebSocket API for the full protocol description.

You should assume:

  • disconnects happen,
  • subscriptions are not durable,
  • there is a propagation delay after resubscribe (join window).

Recommended approach:

  • reconnect with exponential backoff (with a max delay),
  • after reconnect, re-send all subscriptions,
  • consider a small safety delay before you publish critical “first” messages,
  • make consumers idempotent (see Delivery semantics).

Common pitfalls

  • Browser Origin: if allowed_ws_origin is configured, your Origin must match.
  • Slow consumer: if you do heavy work in the receive loop, you may be disconnected.
  • Token expiry: expired tokens fail authentication; refresh/rotate as needed.

See also:

WebSocket client