Attempting to build a custom chat UI using the WebSocket-based Guest API instead of the default widget. The goal is to avoid the widget’s CSS bloat and have full control over the DOM. The initial handshake works, but the connection terminates right after sending the first message payload.
Here is the flow:
- GET /api/v2/conversations/webchat/queues to get the queue id.
- POST /api/v2/conversations/webchat to create the conversation. This returns a
guestToken. - Open WebSocket to
wss://api.mypurecloud.com/api/v2/conversations/webchat/guest/websocket.
The connection opens with readyState: 1. I immediately send the auth payload:
{
"type": "auth",
"guestToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
The server responds with {"type": "auth", "status": "ok"}. The log shows this clearly. However, within 500ms, the onclose event fires with code 1006 (Abnormal Closure). No error message in the payload. Just a silent drop.
I’ve tried adding a keepalive ping every 30 seconds, but the connection dies before that. I’ve also checked the network tab in Chrome. The WebSocket frame looks valid. The guestToken is valid because I can use it to fetch conversation details via REST without issue.
Is there a specific header required in the WebSocket handshake? Or does the Guest API require a specific ping message format immediately after auth? The documentation is sparse on the binary frame structure.
Current JS for the send logic:
ws.send(JSON.stringify({
type: 'message',
body: 'Hello',
timestamp: Date.now()
}));
This never sends because the socket is already closed by the time I try. The auth response comes back, then boom. Connection lost.
What am I missing in the handshake sequence?