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. WebSocket sessions 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, with no socket
to keep alive; a runtime v2 agent answers connect() with an error envelope
explaining the session is SSE-only.
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.
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.