How do I get the number of clients connected to my azure webPubSub?

191 Views Asked by At

I'm trying to build a small app with azure webpubsub. I based it on this tutorial and I'm using javascript.

I started out by creating a function with a binding to my WebPubSub like so:

{
  "disabled": false,
  "bindings": [
    {
      "name": "wpsReq",
      "type": "webPubSubContext",
      "direction": "in"
    },
    { 
      other bindings...
    }
 ]
}

But how do i request the number of clients connected to the instance now? I tried this:

let clients = wpsReq.request.connectionContext.clientConnections.length;

but to no success.

Another way would be through the websocket, but do I have a websocket server when I work with azure webpubsub, and how would i access that?

Thanks in advance for your help.

1

There are 1 best solutions below

0
On

AFAIK there is no way to get the number of connections to your Azure WebPubSub Service directly.

But there are metrics that are published to Azure Monitor, which is what you see in the Azure Portal graphs. You could expose an API that gets this value via the same request that the Azure Portal makes, which is something like this

https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.SignalRService/WebPubSub/{serviceName}/providers/microsoft.Insights/metrics?timespan=2023-01-17T11:30:00.000Z/2023-01-17T17:30:00.000Z&interval=FULL&metricnames=TotalConnectionCount&aggregation=maximum&validatedimensions=false&api-version=2019-07-01