Skip to main content
StructWebSocket is a typed wrapper around the rooms websocket (wss://api.struct.to/ws). It handles connection, keepalive, reconnect, and replay of subscriptions automatically. Every room has a typed filter and a typed subscribe response.

Connect

connect() resolves once the socket reaches the connected state. A socket can safely be kept open for the lifetime of your app; it will auto-reconnect on transient failures.

Subscribe

subscribe(room, filters?) returns a promise that resolves with the server’s subscribe response (rejected filters, current configuration, and so on). Filters are fully typed per room. Some rooms have optional filters. For those, you can omit the second argument to subscribe without any filter:
Calling subscribe a second time on the same room replaces the previous filters.

Listen for events

Register handlers with on(event, listener). Listeners receive fully typed payloads. on returns a disposer function that removes the listener.
You can also use once for one-shot listeners, off to remove a specific listener, and removeAllListeners to clear handlers for a given event (or all events).

Unsubscribe and disconnect

unsubscribe leaves the room and drops its replay entry so it will not be resubscribed on reconnect. disconnect tears down the socket, cancels timers, and clears all state.

Available rooms

See the WebSockets tab for full payload schemas for each room.

Lifecycle events

Reconnection and replay

When the socket drops, the SDK:
  1. Emits disconnected.
  2. Enters reconnecting state with exponential backoff and jitter.
  3. On each attempt, rebuilds the URL via getJwt (if configured) so the latest JWT is used.
  4. On successful reconnect, replays every active subscription so your handlers keep firing without manual bookkeeping.
Configure behaviour via reconnect:

Keepalive

The transport sends a ping every 30 seconds and closes the socket if no pong arrives within 60. This is automatic; you do not need to call it.

Connection state

Possible values: "disconnected", "connecting", "connected", "reconnecting".
Last modified on June 16, 2026