WebSocket reconnection loop on Notification API causing duplicate event bursts

Running into a weird issue with the Notification API WebSocket client in Node.js. The connection drops randomly, which is fine, but my reconnection logic triggers a burst of duplicate events immediately after reconnecting. It’s messing up our real-time dashboard updates.

I’m using the standard @genesyscloud/purecloud-platform-client-notifications-node SDK. Here’s the gist of my setup:

const client = new PureCloudClient();
client.login('username', 'password', 'environment');

const ws = client.getNotificationsApiClient();

ws.on('connect', () => {
 console.log('Connected to Notifications API');
 // Subscribe to conversation events
 ws.subscribe('conversation');
});

ws.on('error', (err) => {
 console.error('WebSocket error:', err.message);
 // Attempt reconnect after 5 seconds
 setTimeout(() => {
 ws.connect();
 }, 5000);
});

The problem is the error event fires, then connect fires, and suddenly I get three or four copies of the same conversation.update event. The SDK docs mention a reconnect option, but it’s not clear if I should be calling ws.connect() manually or letting the SDK handle it internally.

I tried setting ws.setReconnect(true) but that seemed to do nothing. When the connection drops, the error handler runs, waits 5 seconds, and calls connect(). The reconnect works, but the event queue seems to flush old events or duplicate the latest ones.

Is there a specific flag or method I’m missing to prevent this burst? Or should I be using a different approach for handling disconnections? The logs show the connection ID changes, which is expected, but the data payload is identical for the burst events.

Also, the lastEventId isn’t being sent automatically on reconnect, which might be why I’m getting duplicates. How do I capture and reuse that ID in the Node.js SDK? The Java SDK has a clear setLastEventId method, but I can’t find an equivalent in the Node version.

Any code examples for handling this gracefully would be appreciated. Right now, I’m just getting spammed with duplicate updates.