Legacy Dev Forum Posts

 View Only

Sign Up

NotificationHandler reconnect on expired websocket channel

  • 1.  NotificationHandler reconnect on expired websocket channel

    Posted 06-05-2025 18:05

    BenjaminRamsay | 2017-02-20 16:47:59 UTC | #1

    The docs say that WebSocket channels remain active for 24 hours then expire. When using the .NET NotificationHandler helper class, what's the recommended method for identifying when the socket has expired and reconnecting?

    I tried handling the underlying WebSocket OnError and OnClose events, but they did not seem to fire at the 24 hour mark. Is it best to just have a .net timer that fires after 24 hours and reconnects?

    Thanks


    tim.smith | 2017-02-21 15:27:33 UTC | #2

    BenjaminRamsay, post:1, topic:938
    what's the recommended method for identifying when the socket has expired and reconnecting?

    Use the websocket health check: https://developer.mypurecloud.com/api/rest/v2/notifications/notification_service.html. If you get a response, the socket is healthy. If not, you need to reinitialize.

    BenjaminRamsay, post:1, topic:938
    I tried handling the underlying WebSocket OnError and OnClose events, but they did not seem to fire at the 24 hour mark. Is it best to just have a .net timer that fires after 24 hours and reconnects?

    That's correct. The websocket spec definitely has some room for improvement. As a general best practice, I'd suggest resubscribing at 23 hours to ensure there's no race conditions.

    Take note that it's not the websocket that expires at 24 hours, however. It's the subscriptions within PureCloud that expire. You must refresh the subscriptions, not the websocket connection.


    BenjaminRamsay | 2017-02-21 19:03:51 UTC | #3

    Thanks Tim, I assume the way to use the ping/pong health check in the SDK is by calling the underlying NotificationHandler.WebSocket.Ping() method. (Or, it looks like checking the IsActive property would do the same thing)

    I'll also resubscribe every 23 hours as you suggest. Thanks


    tim.smith | 2017-02-21 19:42:12 UTC | #4

    I left the "with the SDK's classes" part out of my reply. Sorry about that. The NotificationHandler.WebSocket.Ping() method is actually something that's part of the Websocket-sharp library and is different than the health check described in the docs. You would need to use NotificationHandler.WebSocket.Send(data) to send the ping request. However, the way NotificationHandler is currently written will cause the response to be suppressed because it doesn't know how to deserialize it (it's not a registered topic).

    I've created API-2325 to add a Ping method to the NotificationHandler class and handle the pong notification. If you need to work around this in the meantime, you can send the message as described above and handle the pong response by registering for the NotificationHandler.WebSocket.OnMessage event to look for the pong message.


    system | 2017-08-28 19:32:14 UTC | #5


    This post was migrated from the old Developer Forum.

    ref: 938