How to get connection ID for Web PubSub

Peter 26 Reputation points
2021-10-16T00:05:25.253+00:00

I am looking at the C# example for publishing messages here: https://learn.microsoft.com/en-us/azure/azure-web-pubsub/tutorial-pub-sub-messages?tabs=csharp#publish-messages-using-service-sdk

The sample uses SendToAllAsync() but I would like to use SendToConnectionAsync(), however I am not sure how to get the connection id. I have opened the live trace tool and see that a connection ID gets logged when the WebsocketClient connects (from this sample: https://learn.microsoft.com/en-us/azure/azure-web-pubsub/tutorial-pub-sub-messages?tabs=csharp#set-up-the-subscriber).

I can take this connection ID and pass it to SendToConnectionAsync() and it works fine but I don't know where to get the connection id programmatically instead of having to copy and paste it from the live trace tool.

Azure Web PubSub
Azure Web PubSub
An Azure service that provides real-time messaging for web applications using WebSockets and the publish-subscribe pattern.
64 questions
0 comments No comments
{count} votes

Accepted answer
  1. Liangying Wei 756 Reputation points Microsoft Employee
    2021-10-20T03:30:20.013+00:00

    There are several ways to get the connection ID:

    1. For the client connection itself, when using the subprotocol, the client receives a connected message once it is connected, which contains the connection Id: https://learn.microsoft.com/azure/azure-web-pubsub/reference-json-webpubsub-subprotocol#connected
    2. For the upstream server side, when the events are triggered, it contains the connectionId in the request header as ce-connectionId, a sample usage about register upstream event handlers and fetch headers using sharp is here: https://learn.microsoft.com/en-us/azure/azure-web-pubsub/tutorial-build-chat?tabs=csharp

    Could you elaborate more about your scenario when you want to SendToConnectionAsync?

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful