Node.js WebSocket consumer dropping events when routing to Kafka

Hey everyone.

We’re trying to set up a Node.js service that subscribes to the Genesys Cloud streaming analytics notifications (specifically the agent-status and interaction-started events) and pipes them directly into a Kafka topic for our WFM adherence calculations. The goal is to have near real-time visibility into agent states without polling the API every few seconds.

The WebSocket connection establishes just fine. I get the initial handshake and the first batch of events flows through perfectly. But after about 30 seconds, the consumer stops receiving any new messages. No error is thrown in the console, and the ping/pong mechanism seems to be working based on the logs, but the message event handler just goes silent.

Here’s the relevant chunk of the Node.js code using the standard ws library:

const WebSocket = require('ws');
const Kafka = require('kafka-node');

const ws = new WebSocket('wss://api.mypurecloud.com/api/v2/analytics/events?topics=agent-status,interaction-started');

ws.on('open', () => {
 console.log('Connected to Genesys Stream');
 // Send OAuth token in the first message as per docs
 ws.send(JSON.stringify({
 'Authorization': 'Bearer ' + process.env.GENESYS_TOKEN
 }));
});

ws.on('message', (data) => {
 try {
 const event = JSON.parse(data);
 console.log('Received event type:', event.type);
 
 // Push to Kafka
 producer.send([{ topic: 'wfm-adherence', messages: JSON.stringify(event) }], (err, data) => {
 if (err) console.error('Kafka send error:', err);
 });
 } catch (e) {
 console.error('Parse error:', e);
 }
});

ws.on('close', (code, reason) => {
 console.log(`Connection closed: ${code} - ${reason}`);
});

The producer is a standard kafka-node high-level producer connected to our internal Kafka cluster. The logs show the connection opening, then a steady stream of events for 30 seconds, then nothing. The close event never fires, so the WebSocket isn’t actually disconnecting. It’s just… quiet.

I’ve tried increasing the keep-alive interval, but that didn’t help. I also noticed that if I remove the Kafka write operation and just console.log the events, the stream continues indefinitely without dropping. This makes me think the Kafka write might be blocking the event loop or causing some backpressure that silently kills the WebSocket listener, but I’m not sure how to debug that.

Has anyone else tried piping Genesys WebSocket events into Kafka with Node.js? Is there something specific about the event stream format I’m missing, or is this a known issue with the ws library handling high-throughput writes?

The token I’m using is a client credentials grant token with the analytics:read scope. I’ve verified the token is valid by making a separate REST call to /api/v2/analytics/events which works fine. Just the streaming part is acting up.