We are building a custom chat interface that bypasses the standard Messenger widget entirely. We are using the Genesys Cloud Guest API to handle the lifecycle of the conversation. The initial handshake works fine. We get the guestToken and connect to the WebSocket endpoint successfully.
The issue happens when the user types rapidly. We are sending typing events via the WebSocket connection. If the user types for more than 10 seconds without sending a message, the connection drops. The server sends a close frame with code 1001, but our client doesn’t receive any error event. The connection just goes silent.
Here is how we are setting up the WebSocket client in our Angular service:
private connectWebSocket(token: string) {
const wsUrl = `wss://{{env}}.mypurecloud.com/api/v2/chat/guests/{{guestId}}/conversation/{{conversationId}}/connect`;
this.ws = new WebSocket(wsUrl);
this.ws.onopen = () => {
console.log('WebSocket connected');
this.sendAuthMessage(token);
};
this.ws.onmessage = (event) => {
const data = JSON.parse(event.data);
this.handleIncomingMessage(data);
};
this.ws.onerror = (error) => {
console.error('WebSocket error', error);
};
this.ws.onclose = (event) => {
console.log('WebSocket closed', event.code, event.reason);
// We try to reconnect here but it fails if the guest token is stale
};
}
When the user resumes typing after the silent period, the send method throws a network error because the socket is in a CLOSED state. We are trying to implement a heartbeat mechanism to keep the connection alive, but the documentation for the Guest API WebSocket protocol is sparse regarding keep-alive strategies.
Is there a specific message type we need to send periodically to prevent the server from pruning idle connections? Or should we be using the REST API to poll for new messages instead of relying on the WebSocket for long idle periods? We prefer the WebSocket for real-time updates, but the disconnects are breaking the UX. Any insight on how other developers handle this specific timeout scenario with the Guest API would be helpful.