Skip to main content
For live chat UIs, the messaging client opens 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.

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:
Envelope typeMeaningdata
threadThe resolved thread id (once per thread){ thread_id }
eventStreaming event; token deltas arrive as thread-message-text-content-updated with the text under data.data.textApollo.StreamEvent
messageTurn complete — the full replyApollo.Message + optional trace_info
errorSession error ({ error, code }) or turn error ({ message, status_code })varies
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.