Azure Web PubSub service client library for .NET

This library can be used to do the following actions. Details about the terms used here are described in Key concepts section.

  • Send messages to hubs and groups.
  • Send messages to particular users and connections.
  • Organize users and connections into groups.
  • Close connections
  • Grant, revoke, and check permissions for an existing connection

Source code | Package | API reference documentation | Product documentation | Samples

Getting started

Install the package

Install the client library from NuGet:

dotnet add package Azure.Messaging.WebPubSub --prerelease


Authenticate the client

In order to interact with the service, you’ll need to create an instance of the WebPubSubServiceClient class. To make this possible, you’ll need the connection string or a key, which you can access in the Azure portal.

Create a WebPubSubServiceClient

var serviceClient = new WebPubSubServiceClient(new Uri("<endpoint>"), "<hub>", new AzureKeyCredential("<access-key>"));

Key concepts


Connections, represented by a connection id, represent an individual websocket connection to the Web PubSub service. Connection id is always unique.


Hub is a logical concept for a set of connections. Connections are always connected to a specific hub. Messages that are broadcast to the hub are dispatched to all connections to that hub. Hub can be used for different applications, different applications can share one Azure Web PubSub service by using different hub names.


Group allow broadcast messages to a subset of connections to the hub. You can add and remove users and connections as needed. A client can join multiple groups, and a group can contain multiple clients.


Connections to Web PubSub can belong to one user. A user might have multiple connections, for example when a single user is connected across multiple devices or multiple browser tabs.


Using this library, you can send messages to the client connections. A message can either be string text, JSON or binary payload.


Broadcast a text message to all clients

```C# Snippet:WebPubSubHelloWorld var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), “some_hub”, new AzureKeyCredential(key));

serviceClient.SendToAll(“Hello World!”);

### Broadcast a JSON message to all clients

```C# Snippet:WebPubSubSendJson
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));

            Foo = "Hello World!",
            Bar = 42

Broadcast a binary message to all clients

```C# Snippet:WebPubSubSendBinary var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), “some_hub”, new AzureKeyCredential(key));

Stream stream = BinaryData.FromString(“Hello World!”).ToStream(); serviceClient.SendToAll(RequestContent.Create(stream), ContentType.ApplicationOctetStream); ``` ``


Setting up console logging

You can also easily enable console logging if you want to dig deeper into the requests you’re making against the service.

Find an issue? Please report it here. Your feedback is highly appreciated! Provide it via Azure Feedback Forum.