Node.js WebSocket reconnect loop hitting 429 on /api/v2/analytics/wem/realtime

We are running a Node.js service to pull WEM adherence metrics in real-time for our West Coast team. The service subscribes to the Notification API via WebSocket. When the connection drops, the client attempts to reconnect immediately. This is causing a burst of requests that Genesys Cloud rejects with a 429 Too Many Requests status. The WebSocket handshake itself isn’t failing, but the subsequent subscription setup hits rate limits because we are trying to re-establish the session too quickly after a disconnect.

Here is the simplified reconnection logic we are using:

let ws;

function connect() {
 ws = new WebSocket('wss://api.mypurecloud.com/api/v2/analytics/wem/realtime');
 
 ws.onopen = () => {
 console.log('Connected');
 // Send subscription message here
 };

 ws.onclose = (event) => {
 console.log(`Connection closed: ${event.code}`);
 // Immediate reconnect attempt
 setTimeout(connect, 1000); 
 };

 ws.onerror = (error) => {
 console.error('WebSocket Error', error);
 };
}

The logs show a rapid cycle of connections and 429 errors when the network flutters. We need to implement exponential backoff to avoid hammering the API. Is there a recommended retry interval strategy for the WEM WebSocket endpoint? Also, does the onclose event provide a reason code that indicates if the disconnect was intentional (like a session expiry) versus a network glitch? We want to distinguish between those cases. Currently, we just treat all closures the same way.

The 429 response includes a Retry-After header, but parsing that in the WebSocket context feels messy since it’s part of the HTTP upgrade handshake failure, not the data stream. How are others handling this? We just want the metrics to flow without triggering abuse detection on our API keys. The service is critical for our SLA reporting. Every minute of downtime means missed adherence data for 500 agents. We’ve tried increasing the timeout to 5 seconds, but it still feels fragile during peak load times.