Node.js WebSocket reconnection dropping subscription state on /v2/analytics/queue/metrics

Problem
The dashboard team needs real time queue layout updates. We built a Node.js service that opens a WebSocket to wss://api.mypurecloud.com/v2/analytics/queue/metrics. The connection holds steady for about an hour. Then the local network blips. The reconnect routine fires. It pulls the socket back online. The subscription state resets anyway. We can’t keep the rolling window for the analytics export. The admin UI cache ends up stale.

Code

const WebSocket = require('ws');
const ws = new WebSocket('wss://api.mypurecloud.com/v2/analytics/queue/metrics', {
 headers: { 'Authorization': `Bearer ${token}` }
});

ws.on('open', () => {
 ws.send(JSON.stringify({
 eventType: 'queueMetrics',
 queues: ['q-12345', 'q-67890'],
 metrics: ['longestHoldTime', 'agentsAvailable']
 }));
});

ws.on('close', () => {
 setTimeout(() => {
 const retryWs = new WebSocket('wss://api.mypurecloud.com/v2/analytics/queue/metrics', {
 headers: { 'Authorization': `Bearer ${token}` }
 });
 retryWs.on('open', () => retryWs.send(JSON.stringify({ eventType: 'queueMetrics' })));
 }, 5000);
});

Error
The socket reconnects without throwing a 401. The subscription payload just disappears. No 1000 Normal Closure code shows up in the server logs. We get a blank state on the next poll cycle.

Question
How do we preserve the subscription JSON across the reconnect attempt without hammering the endpoint? Should the retry logic resend the full object or is there a specific header we are missing? The documentation mentions session persistence but the ws library doesn’t handle it automatically. We’ve tried storing the message in a closure. It’s still dropping the rolling window metrics.