PeterM | 2019-12-09 03:55:15 UTC | #1
How exactly do you use Ping & Pong in .NET SDK v68.0.1?
I've followed the thread that lead to API-2325 being created but can't figure out how I can implement Ping and Pong to check a WebSocket is alive.
When I call NotificationHandler.Ping method I'm expecting the NotificationReceivedHandler created when my NotificationHandler is instantiated to be fired but nothing happens. Do I have to dive into the NotificationHandler.WebSocket property to check for the Pong?
I'd be grateful for a high level description of how to implement Ping & Pong 'cause it's got me a bit stumped. Thanks
tim.smith | 2019-12-09 15:53:24 UTC | #2
To send a ping, use the ping() method on NotificationHandler. The description for the method notes how to handle the pong:
Must be subscribed using topic "channel.metadata" and handle with type ChannelMetadataNotification.
PeterM | 2019-12-09 22:32:41 UTC | #3
Thanks Tim
The Ping metadata summary wasn't available in .NET SDK 68.0.1 so that's why I'd missed it. I've now updated to 71.0.0 and can see it. "channel.metadata" and "ChannelMetadataNotification" are also not included in the NotificationTopics class which also confused me.
Based on your response, I've now got Ping working. When I call Ping I get a Pong back immediately but then I've also noticed that ChannelMetadataNotification's with Pongs keep coming every 30 secs. I was expecting to have to initiate my own Ping/Pong cycle when required.
Can I assume that if I renew the channel.metadata subscription every 24hrs (like all other subscriptions), and if all network infrastructure is working OK, Pongs will continually be received each 30 secs?
tim.smith | 2019-12-09 23:20:04 UTC | #4
PeterM, post:3, topic:6665
Can I assume that if I renew the channel.metadata subscription
You don't technically subscribe to channel.metadata like you do other topics; all channels will receive notifications on that topic automatically. The SDK's add subscription method obscures this nuance (check out the source above to see how). Heartbeats will occur every 30 seconds. The ping/pong feature is separate from the heartbeats for the purpose of allowing an app to actively validate communication with the server, e.g. initially when the websocket is connected.
If you haven't seen it already, there's general documentation on notifications behavior here: https://developer.mypurecloud.com/api/rest/v2/notifications/notification_service.html. Side note, I've logged an issue to clarify some points in that and the SDK's documentation based on this discussion.
PeterM | 2019-12-11 03:46:51 UTC | #5
Sorry - still trying to get this right in my head ...
If I create a channel and add a subscription to channel.metadata then, yes, I start receiving heartbeats every 30 secs as per https://developer.mypurecloud.com/api/rest/v2/notifications/notification_service.html.
However you've said:
You don't technically subscribe to channel.metadata like you do other topics; all channels will receive notifications on that topic automatically.
Your comment leads me to believe that if I subscribe for, say, v2.users.{0}.activity then the handler I setup for that will receive both the subscribed AgentActivityChangedTopicAgentActivity and ChannelMetadataNotifications (for the heartbeats). That's not the case and is not how its described in the "Use the Notification Service" URL above and the way you've described it.
I do seem to have to subscribe to channel.metadata.
tim.smith | 2019-12-11 22:47:09 UTC | #6
There are two pieces that are involved in this, the notification service itself and the .NET SDK.
The notification service will send the heartbeat messages to every connected websocket. This behavior is not impacted by any subscriptions made to the channel or by using the SDK. You can see this using the Dev Tools Notification Tester (wait ~30 seconds after page load for the first heartbeat to come in; the full notification payload is traced out in the JS console).
The SDK adds a layer of logic on top of the notifications for convenience. One behavior that it adds is that you can only receive a callback in your code for an event if a Type has been associated with the topic using AddSubscription or AddSubscriptions. The SDK's websocket handler will always receive channel.metadata notification events, but the handler in your code won't be raised unless a Type has been associated with the topic.
PeterM, post:5, topic:6665
I do seem to have to subscribe to channel.metadata.
Not for the notification service to send it. But you do have to subscribe for your event handler to receive it.
PeterM | 2019-12-12 00:13:42 UTC | #7
Thanks Tim. I'd arrived at this conclusion myself with a bit of thinking and experimenting. I appreciate your help.
Aleks | 2019-12-18 06:27:43 UTC | #8
Hi! https://github.com/websockets/ws/issues/1521 - maybe this information may help you.
PeterM | 2019-12-18 07:05:46 UTC | #9
Thanks - I've got it sorted now and understand how the Ping/Pong and Heartbeat functionality is working with the .NET SDK.
system | 2020-01-18 07:05:51 UTC | #10
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 6665