I am building a ServiceNow integration that subscribes to Genesys Cloud events via the Notification API (/api/v2/notifications). I have the OAuth client credentials flow working and can successfully open a WebSocket connection. However, I am struggling with the reconnection logic when the connection drops unexpectedly or when the subscription token expires.
Currently, I am using the ws library in Node.js. My initial implementation listens for the close event and attempts to reconnect immediately. The issue is that the subscription token obtained from /api/v2/notifications/subscription has a limited lifespan (usually 24 hours). If my Node.js process stays up for longer than the token validity, the reconnection attempt fails with a 401 Unauthorized error because the old token is invalid. I have to re-fetch the token before reconnecting, but handling the async nature of the token refresh inside the close event handler is causing race conditions or stale closures.
Here is a simplified snippet of my current approach:
let ws;
let subscriptionToken;
function connect() {
ws = new WebSocket(`wss://api.mypurecloud.com/api/v2/notifications?token=${subscriptionToken}`);
ws.on('close', (code, reason) => {
console.log('Connection closed:', code, reason);
// Problem: How do I ensure I have a fresh token before reconnecting?
// If I just call connect() here, it uses the old token.
setTimeout(connect, 5000);
});
}
I have tried wrapping the connect function to check token expiry, but the WebSocket library doesn’t provide a hook to intercept the authentication phase. I also noticed that Genesys Cloud sends a specific close code or message when a token is expired, but I am not sure if I can reliably parse that before the connection fully terminates.
Is there a recommended pattern or SDK helper for managing token refresh and automatic reconnection for the Notification API in a long-running Node.js process? I want to avoid polling for tokens unnecessarily but also prevent connection failures due to expired credentials.
What is the correct way to handle automatic WebSocket reconnection and token refresh for the Genesys Cloud Notification API in Node.js to ensure high availability without race conditions?