Node.js WebSocket consumer dropping analytics notification payloads on reconnect

The WebSocket connection to the Notification API is dropping the analytics:queue:realtime stream right after a heartbeat timeout. The reconnect logic is sending a stale lastEventId back to the server, which causes the payload to skip the last forty-five seconds of queue metrics. We’re trying to stream this data into a Kafka topic using a custom Node.js consumer, but the data gap is messing up the downstream aggregation. The timestamps are all over the place.

The code is set up to listen on wss://api.mypurecloud.com/api/v2/analytics/streams. We’ve tried resetting the eventID in the catch block, but the server still returns a 400 Bad Request on the reconnect attempt. The error message says the eventID is invalid or out of range. Makes no sense since we’re just echoing the last one.

const WebSocket = require('ws');
const ws = new WebSocket('wss://api.mypurecloud.com/api/v2/analytics/streams?metrics=waitTimeInQueue');

ws.on('open', () => {
 console.log('Stream open');
});

ws.on('message', (data) => {
 const payload = JSON.parse(data);
 producer.send({ topic: 'genesys-analytics', messages: [{ value: data }] });
});

ws.on('close', () => {
 console.log('Stream closed, reconnecting...');
 setTimeout(() => connect(), 1000);
});

The issue seems to be how we’re handling the reconnect payload. We’re passing the last received eventID as a query parameter, but the stream isn’t resuming correctly. The admin UI shows the queue metrics updating fine, so the data is there. Just not flowing through the WebSocket consumer.

Tried adding the Authorization header to the WebSocket upgrade request. Got a 401 Unauthorized. Switched to using the access_token in the query string. That works for the initial connection. The reconnect is where it breaks. The reconnect payload keeps failing validation. Not sure what format the server expects.