Node.js WebSocket consumer dropping Analytics events before Kafka publish

I’m building a sidecar service in Node.js to consume Genesys Cloud analytics notifications via WebSocket and forward them to a local Kafka topic. The goal is to get real-time queue stats into our data lake without polling the REST API every second.

I’ve got the connection established using the ws library, and I’m subscribing to the analytics/queues event type. The initial handshake returns a 101 Switching Protocols, so auth is fine. The issue is reliability. About 30% of the events seem to vanish before they hit the Kafka producer.

Here’s the relevant snippet:

ws.on('message', (data) => {
 const event = JSON.parse(data);
 if (event.type === 'analytics/queues') {
 producer.send({
 topic: 'gen-analytics',
 messages: [{ value: JSON.stringify(event) }]
 }).catch(err => console.error('Kafka error:', err));
 }
});

I’m not seeing any errors in the Kafka producer logs. The ws connection stays up, but the event count in Kafka is way lower than what I see in the Genesys UI. Is there a backpressure issue I need to handle in the WebSocket listener? Or am I missing a specific header in the subscription request to ensure all events are pushed? I’ve tried increasing the buffer size in the ws config but that didn’t help.

scripts don’t do WebSockets. You’re fighting the wrong tool if you think can handle this. But since you’re on Node, the issue is likely heartbeat handling or reconnection logic, not the endpoint.

Genesys Cloud WS connections drop if you don’t handle the ping/pong frames correctly. The ws library doesn’t auto-respond to pings by default in all versions. You need to listen for ping and send pong manually, or use a library that handles it. Also, ensure you’re subscribing to the correct scope.

Here’s a quick Node to handle the heartbeat and reconnection. It’s messy but works.

const WebSocket = require('ws');

function connect() {
 const ws = new WebSocket('wss://api.niceincontact.com/api/v2/analytics/notifications/realtime');
 
 ws.on('open', () => {
 // Subscribe to queues
 ws.send(JSON.stringify({
 "event": "subscribe",
 "data": { "entityId": "your-queue-id" }
 }));
 });

 ws.on('ping', (data) => {
 ws.pong(data); // Critical for keeping connection alive
 });

 ws.on('message', (data) => {
 // Parse and send to Kafka
 });

 ws.on('close', () => {
 console.log('Disconnected. Reconnecting...');
 setTimeout(connect, 5000);
 });
}

connect();

Check your Kafka producer buffer too. If it backs up, the WS message queue fills and drops events.