Configuration object (see WSClientConfig for details)
Current connection state.
True when the socket is in the OPEN state and ready for data.
Number of messages currently buffered in the outgoing queue.
Snapshot of lifetime metrics (bytes sent/received, reconnect count, uptime).
Read-only list of currently subscribed rooms.
Current backpressure snapshot.
isBackpressured is true when buffered bytes >= highWaterMark.
The server endpoint the client is currently (or was last) connected to. Populated after a successful handshake. Useful for verifying sticky-session routing in load-balanced deployments.
Initiate the WebSocket connection. Resolves once the handshake completes. If already CONNECTING or RECONNECTING, queues the caller for resolution once OPEN is reached.
A promise that resolves when the socket is OPEN
Returns a Promise that resolves once the socket reaches OPEN. Safe to call while CONNECTING or RECONNECTING. Rejects with WSConnectTimeoutError if timeoutMs elapses (default 60s).
Timeout in milliseconds. If 0, no timeout is applied and the promise will wait indefinitely until the socket opens or errors.
A promise that resolves when the socket is OPEN
Permanently close the connection. No reconnect will occur.
Destroy the client and clean up all resources. More aggressive than close() - clears all listeners and buffers.
Send a text message. Buffered when the socket is reconnecting or under backpressure.
Text payload to send
Send a binary message. Buffered when the socket is reconnecting or under backpressure.
Binary payload (Uint8Array or ArrayBuffer)
Serialize and send a value as JSON text.
Any JSON-serializable value
Send a message and await a correlated reply.
What to send (string → sent as-is; anything else → JSON-serialized).
Predicate called for every incoming message until it returns true.
Transform the matching WSMessage into the return type. Default: msg.json ?? msg.data.
How long to wait ms. Default: 10_000. 0 = no timeout.
A promise that resolves with the extracted reply value
Register a message listener.
Callback invoked for every incoming message
An eject function that removes this listener
Register a close listener.
Callback invoked with the close event (code + reason)
An eject function that removes this listener
Return and clear the current outgoing message buffer.
When reconnecting, any messages queued while disconnected are held in this buffer. On successful reconnect, buffered messages are automatically flushed to the new socket. Use this method to inspect or discard queued messages if needed.
Array of pending messages (strings or ArrayBuffers)
Subscribe to a room. Sends a room:join signal over the wire and tracks
the subscription so it can be re-joined after a reconnect.
Room name.
Optionalnamespace: stringOptional namespace qualifier.
Unsubscribe from a room. Sends a room:leave signal.
Room name to leave
Optionalnamespace: stringOptional namespace qualifier
Returns a Promise that resolves when the outgoing buffer has drained below the low-water mark. Rejects immediately if already closed/aborted.
Max ms to wait. 0 = no timeout. Default: 30_000.
A promise that resolves when the buffer is drained
Gracefully shut down: wait for the outgoing buffer to drain, then close the connection cleanly.
Max ms to wait for drain before force-closing. Default: 30_000.
A promise that resolves after clean shutdown
Iterate over incoming messages. The iterator closes when the socket closes.
WebSocket client with reconnection, backoff, ping/pong, and buffering.
Cross-runtime: Browser, Deno, Bun, Node.js, Cloudflare Workers, Vercel Edge.
Architecture:
Example