AppFoundry WebSocket reconnects killing Cognigy bot latency

Building a custom AppFoundry widget in C# that bridges Genesys Cloud events to a local NICE Cognigy instance via WebSocket. The goal is to push real-time transcript updates to the bot engine without polling.

Using the standard genesyscloud-websocket-sdk pattern but hitting a wall with connection stability. The socket drops every 60 seconds. The docs say the connection should stay open indefinitely unless the tenant sends a close frame. Here is the reconnect logic:

var client = new WebSocketClient(new Uri("wss://api.mypurecloud.com/api/v2/analytics/events");
client.OnMessage += async (sender, e) => {
 // Parse JSON and forward to Cognigy API
};
client.OnClose += (sender, e) => {
 Console.WriteLine($"Closed: {e.Reason}");
 Task.Delay(1000).Wait(); // Bad practice but debugging
 Connect();
};

The issue is the latency spike. When the socket reconnects, there is a 2-3 second gap where transcript events are lost or duplicated. The Cognigy bot gets out of sync with the live call.

I tried increasing the keep-alive timeout in the HttpClient handler but it’s a WebSocket, not HTTP. The OnClose event fires with reason 1001 Going Away which usually means the server is arting or rotating keys.

Is there a specific header or payload I need to send to keep the Genesys side alive? The docs for /api/v2/analytics/events are sparse on client-side keep-alive strategies. Currently, the bot UI freezes during these drops.

Also, the token refresh via OAuth2 is handled separately but I suspect the token expiry might be triggering the drop. The access token lasts 30 mins. The drops happen way more often.

Any ideas on how to handle the reconnect gracefully without losing the event stream context?