Skip to main content
For runtime v1 agents, the messaging client can open a bidirectional WebSocket session: send message frames, receive the reply as a token stream, and recover missed events after a reconnect. Authentication is handled for you, and it works in both Node and the browser.
Runtime v1 agents only. Sessions — turns sent and answered over the socket — belong to runtime v1. Runtime v2 agents stream over HTTP instead: one streamMessage call per turn gives you the same token-by-token reply, resumable with Last-Event-ID. A runtime v2 agent answers a message frame with an error envelope explaining it is SSE-only, and uses the socket for one thing: to watch a conversation live. Runtime v1 agents maps each socket frame to its streamMessage equivalent. (Tip: UUID thread ids mean runtime v2.)

Open a session

connect() accepts optional arguments:

Send a message

The socket sends typed frames. A message frame submits a turn — note that on the WebSocket the target agent is explicit; use the id resolved from your key:

Receive envelopes

Every server frame is a sequenced envelope. Register handlers with on(event, handler) — events are open, message, error, and close:
on() registers a single handler per event — calling it again for the same event replaces the previous handler.
The same handler also receives the frames of a watched conversation: watching, and message / silent frames that carry sequence instead of seq. That is why the example checks 'seq' in envelope before reading it. From SDK 3.4.2 the check is required for the handler to compile.

Resume after a reconnect

Envelopes carry a monotonic seq (locally generated errors use -1). Track the highest value you’ve processed; after a reconnect, ask the server to replay what you missed instead of re-running the turn:
The socket reconnects automatically (up to reconnectAttempts), so a typical pattern is to send a resume frame from the open handler when lastSeq > -1.

Close the session

The socket also exposes readyState for connection-state checks.

Full example

The wire protocol underneath (URL, query parameters like include_trace and events, close codes) is documented in API → WebSocket, with the machine-readable contract published as an AsyncAPI document.