question

Peter-4112 avatar image
0 Votes"
Peter-4112 asked Peter-4112 commented

How to get connection ID for Web PubSub

I am looking at the C# example for publishing messages here: https://docs.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://docs.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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

vicancy avatar image
1 Vote"
vicancy answered Peter-4112 commented

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://docs.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://docs.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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your response. After exploring Web PubSub a little more, I learned that it made more sense for my use case to actually use SendToUserAsync instead of a per-connection basis. I was initially looking at the connectionId stuff because I noticed a lot of the library functions required the connectionId.

1 Vote 1 ·