WebSocket connection
After creating a session, open a WebSocket to send and receive events in real time.
Connecting
wss://<base-url>/ws
Query parameters
The access token is passed as a query parameter (not a header) because WebSocket connections do not support custom headers during the handshake. All connections use TLS encryption.
Connection limits
What happens on connect
When the WebSocket opens, the server immediately sends the session history as one or more EVENT_TYPE_EVENT_BATCH events. This lets your client reconstruct the conversation state — especially useful when reconnecting after a network drop.
The first batch always contains at least the EVENT_TYPE_SESSION_START event, which tells your client about the session’s capabilities (e.g. whether streaming is enabled).
After processing the history:
- New session (no
EVENT_TYPE_POLY_AGENT_JOINEDin the replayed events): sendEVENT_TYPE_REQUEST_POLY_AGENT_JOINto start the conversation. - Reconnect: the agent has already joined — skip the join request.
Keeping the connection alive
Send EVENT_TYPE_HEARTBEAT events at regular intervals to prevent the connection from timing out. The server echoes each heartbeat back.
If the server receives no messages (including heartbeats) for the idle timeout period (10 minutes), the connection is closed and the session will eventually end.
Read capabilities.heartbeat_interval_seconds from the EVENT_TYPE_SESSION_START event and use that interval. Fall back to 30 seconds if it’s not set.
Reconnecting
If the connection drops, reconnect using the same session_id and access_token. Pass the cursor parameter to avoid replaying events you’ve already received:
Set lastSequence to the highest sequence number your client has seen. The server replays only events after that point.
If the cursor value can’t be parsed (e.g. non-numeric), the server logs a warning and defaults to 0 (full replay) instead of rejecting the connection. Your client stays connected — you just receive the full history.
Connection dropped ≠ session ended. If the WebSocket closes unexpectedly, the session is still alive on the server. Reconnect with the same session_id and a cursor to resume. Do not create a new session on reconnect.
Recommended reconnect strategy
- Implement exponential backoff: start at 1 second, cap at 30 seconds
- Use
capabilities.max_reconnect_attemptsfromSESSION_STARTas a hint for how many times to retry - After exhausting retries, surface an error and let the user start a new session

