Building a Node.js service to subscribe to Genesys Cloud Analytics Notification events via the WebSocket API. The goal is to stream these events directly into a Kafka topic for downstream processing.
The connection establishes fine. I get the initial heartbeat and the first batch of events. The issue is reliability. If no analytics events occur for roughly 120 seconds, the WebSocket connection drops silently. The close event fires with code 1006 (Abnormal Closure). My reconnect logic handles this, but it introduces a 2-3 second gap in data ingestion, which is unacceptable for our real-time dashboards.
I’m using the standard ws library. Here’s the relevant setup:
const WebSocket = require('ws');
const kafka = require('kafka-node');
const wsUrl = 'wss://api.mypurecloud.com/api/v2/analytics/notifications/events';
const headers = {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
};
const ws = new WebSocket(wsUrl, { headers });
ws.on('open', () => {
console.log('Connected to Analytics Notifications');
// Subscribe to specific event types
const subscribeMsg = {
"type": "subscribe",
"eventTypes": ["agent-stat", "queue-stat"]
};
ws.send(JSON.stringify(subscribeMsg));
});
ws.on('message', (data) => {
const event = JSON.parse(data);
// Push to Kafka
producer.send([{ topic: 'gen-analytic-events', messages: [data] }], (err, data) => {
if (err) console.error('Kafka error:', err);
});
});
ws.on('close', (code, reason) => {
console.log(`Connection closed: ${code} - ${reason}`);
// Reconnect logic here...
});
I’ve tried sending a custom ping frame every 30 seconds from the client side, but the server seems to ignore it or the connection still terminates. The documentation mentions a keep-alive mechanism, but I don’t see a specific header or message type to explicitly configure the timeout on the server side via the WebSocket.
Is there a specific subscription parameter I’m missing to extend the idle timeout? Or is this a known limitation where I need to handle the reconnect gap differently? The Authorization header refreshes are handled separately, so that’s not the issue.