Hey everyone. I’m trying to build a simple Node.js script to listen to the Genesys Cloud Notification API via WebSocket. It connects fine initially, but when I intentionally close the connection or the server drops it, my reconnection logic isn’t working as expected. I’m getting a close event, but when I try to reconnect, it seems to hang or fail silently without throwing an error. Here’s the basic setup I’m using:
const WebSocket = require('ws');
let ws = new WebSocket('wss://api.us.genesys.cloud/platform/v1/notificator');
ws.on('open', () => {
console.log('Connected');
ws.send(JSON.stringify({
"topics": ["routing.queue.event"],
"mode": "subscribe"
}));
});
ws.on('close', () => {
console.log('Connection closed. Reconnecting in 5s...');
setTimeout(() => {
ws = new WebSocket('wss://api.us.genesys.cloud/platform/v1/notificator');
// Re-attach listeners here?
}, 5000);
});
Is there a specific header or auth token I need to include on the reconnect attempt? The initial connection uses an OAuth token, but the WebSocket spec for Genesys isn’t super clear on how to pass that back in after a reconnect. Also, should I be re-subscribing to the topics every time? I noticed the docs mention a heartbeat but I’m not sure if that’s handled automatically. Any code examples for a solid reconnect pattern would be appreciated. I’ve been staring at this for an hour.