Node.js WebSocket consumer for CXone Streaming Analytics dropping events

Just noticed that my Node.js consumer is intermittently failing to parse the binary frames from the CXone Streaming Analytics WebSocket endpoint.

I have successfully authenticated using client_credentials and established the connection to wss://api-us.nice-incontact.com/api/v2/analytics/streaming. The connection opens with a 101 Switching Protocols response, and I receive the initial metadata frame correctly. However, when data events start flowing, specifically around high-volume intervals, the ws.on('message') callback receives Buffer objects that do not decode cleanly to JSON. I am using the standard ws library in Node.js v18. Here is the relevant consumer logic:

ws.on('message', (data) => {
 try {
 const payload = JSON.parse(data.toString('utf8'));
 console.log('Event:', payload.eventType);
 } catch (err) {
 console.error('Parse error:', err.message);
 // Attempting to log raw buffer
 console.log('Raw:', data);
 }
});

The error logged is typically SyntaxError: Unexpected token '�' in JSON at position 0. I suspect the streaming API might be using a specific compression or framing format that requires explicit decompression before parsing, or perhaps the WebSocket handshake headers need a specific Accept-Encoding parameter. Has anyone successfully consumed these binary frames in Node.js? Is there a specific CXone header required during the WebSocket upgrade to ensure UTF-8 text frames instead of binary?

Make sure you handle the binary frames as raw buffers, not strings. The streaming protocol sends JSON wrapped in a length-prefixed binary frame, so standard ws.on('message', (data) => console.log(data)) will fail on the payload.

ws.on('message', (data, isBinary) => {
 if (isBinary) {
 const payload = data.slice(4).toString('utf8'); // Skip 4-byte length prefix
 const event = JSON.parse(payload);
 console.log(event);
 }
});

This mirrors how we handle raw data in Five9 scripts where you have to manually parse the header. Did you check the frame size against the prefix?