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 type | Meaning | data |
|---|
thread | The resolved thread id (once per thread) | { thread_id } |
event | Streaming event; token deltas arrive as thread-message-text-content-updated with the text under data.data.text | Apollo.StreamEvent |
message | Turn complete — the full reply | Apollo.Message + optional trace_info |
error | Session 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.