Chat with an agent over a socket, as a visitor

wss://api.hilus.ai/Platform/chat/secure-stream

Chat with an agent over a socket, as a visitor.

The socket behind the hosted chat page and the embed; use it to build your own visitor chat. Authenticate with the chat's share slug as ?token= (a public capability, safe in a browser), or with a chat session or resume token from the hosted page. With a slug, the first resume frame may name a conversation the same visitor had within the flow's resume window (with its visitor_id); otherwise a new conversation starts. A resume token is locked to its one conversation and cannot start a new one. The frames are the same as on the authenticated chat socket.

Side effects: every turn runs the agent's tools for real and stores the conversation.

Args: token: The chat's share slug (sh_...) or a session token.

Query parameters

Heartbeat: the server pings every 25.0s; answer each with {"type": "pong"}. Idle timeout: 900.0s without a client frame closes with 4002.

Close codes

Code Meaning
1000 Normal close; also sent when the client stops answering heartbeat pings.
1003 No WebSocket route at this path.
1008 Policy violation: credential not accepted for this route, missing scope or role, or invalid connection parameters (the reason names them).
1011 Server error while handling the connection.
1012 Server restarting; reconnect.
1013 Rate or concurrency limit reached; retry later.
4001 Session expired or missing; re-authenticate (reload) and reconnect.
4002 Closed for inactivity (no real client frame within the idle timeout); reconnect at once only to resume a conversation the server has stored, otherwise when the user acts again.
4003 Credential of the wrong kind for this route; do not retry with the same credential.

Example

// Mint a short-lived session token on your server; never put an API key in a browser or a URL.
const socket = new WebSocket("wss://api.hilus.ai/Platform/chat/secure-stream?token=" + HILUS_SESSION_TOKEN);