Kafka consumer dropping CXone streaming analytics events - deserialization error?

Trying to build a Node.js consumer for the CXone streaming analytics notification events. The goal is to push these directly to a Kafka topic for downstream processing.

The WebSocket connection to the notification endpoint seems stable. I’m getting the events, but when I try to parse the payload to create the Kafka message, the consumer crashes on specific event types.

Here’s the relevant :

ws.on('message', (data) => {
 try {
 const event = JSON.parse(data);
 if (event.type === 'analytics-update') {
 kafkaProducer.send({
 topic: 'cxone-metrics',
 messages: [{ value: JSON.stringify(event) }]
 });
 }
 } catch (e) {
 console.error('Parse error:', e);
 }
});

The error log shows: SyntaxError: Unexpected token o in JSON at position 1. This happens intermittently. Sometimes it works for 100 events, then fails.

I checked the raw data string before parsing. It looks valid. Is the WebSocket library returning an object instead of a string sometimes? Or is CXone sending fragmented frames that Node.js isn’t reassembling correctly before JSON.parse runs?

Also, the event payload structure changes slightly between interaction-start and interaction-end. Should I be handling those differently before sending to Kafka? The current code treats all analytics events the same.

You’re probably hitting the data field being a Buffer, not a string. Cast it first.

ws.on('message', (data) => {
 const msg = JSON.parse(data.toString());
 // process msg
});