![]() |
VOOZH | about |
dotnet add package Azure.Messaging.WebPubSub --version 1.6.0
NuGet\Install-Package Azure.Messaging.WebPubSub -Version 1.6.0
<PackageReference Include="Azure.Messaging.WebPubSub" Version="1.6.0" />
<PackageVersion Include="Azure.Messaging.WebPubSub" Version="1.6.0" />Directory.Packages.props
<PackageReference Include="Azure.Messaging.WebPubSub" />Project file
paket add Azure.Messaging.WebPubSub --version 1.6.0
#r "nuget: Azure.Messaging.WebPubSub, 1.6.0"
#:package Azure.Messaging.WebPubSub@1.6.0
#addin nuget:?package=Azure.Messaging.WebPubSub&version=1.6.0Install as a Cake Addin
#tool nuget:?package=Azure.Messaging.WebPubSub&version=1.6.0Install as a Cake Tool
Azure Web PubSub Service is an Azure-managed service that helps developers easily build web applications with real-time features and publish-subscribe pattern. Any scenario that requires real-time publish-subscribe messaging between server and clients or among clients can use Azure Web PubSub service. Traditional real-time features that often require polling from server or submitting HTTP requests can also use Azure Web PubSub service.
You can use this library in your app server side to manage the WebSocket client connections, as shown in below diagram:
Use this library to:
Details about the terms used here are described in Key concepts section.
Source code | Package | API reference documentation | Product documentation | Samples
Install the client library from NuGet:
dotnet add package Azure.Messaging.WebPubSub
WebPubSubServiceClientIn 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 that will authenticate using a key credential.
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
A connection, also known as a client or a client connection, represents an individual WebSocket connection connected to the Web PubSub service. When successfully connected, a unique connection ID is assigned to this connection by the Web PubSub service.
A hub is a logical concept for a set of client connections. Usually you use one hub for one purpose, for example, a chat hub, or a notification hub. When a client connection is created, it connects to a hub, and during its lifetime, it belongs to that hub. Different applications can share one Azure Web PubSub service by using different hub names.
A group is a subset of connections to the hub. You can add a client connection to a group, or remove the client connection from the group, anytime you want. For example, when a client joins a chat room, or when a client leaves the chat room, this chat room can be considered to be a group. 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.
When a client is connected, it can send messages to the upstream application, or receive messages from the upstream application, through the WebSocket connection.
// Generate client access URI for userA
serviceClient.GetClientAccessUri(userId: "userA");
// Generate client access URI with initial permissions
serviceClient.GetClientAccessUri(roles: new string[] { "webpubsub.joinLeaveGroup.group1", "webpubsub.sendToGroup.group1" });
// Generate client access URI with initial groups to join when the connection connects
serviceClient.GetClientAccessUri(groups: new string[] { "group1", "group2" });
var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub");
serviceClient.SendToAll("Hello World!");
var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub");
serviceClient.SendToAll(RequestContent.Create(
new
{
Foo = "Hello World!",
Bar = 42
}),
ContentType.ApplicationJson);
var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub");
Stream stream = BinaryData.FromString("Hello World!").ToStream();
serviceClient.SendToAll(RequestContent.Create(stream), ContentType.ApplicationOctetStream);
Azure Web PubSub supports OData filter syntax to filter out the connections to send messages to.
Details about filter syntax please see OData filter syntax for Azure Web PubSub.
var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub");
// Use filter to send text message to anonymous connections
serviceClient.SendToAll(
RequestContent.Create("Hello World!"),
ContentType.TextPlain,
filter: ClientConnectionFilter.Create($"userId eq {null}"));
// Use filter to send JSON message to connections in groupA but not in groupB
var group1 = "GroupA";
var group2 = "GroupB";
serviceClient.SendToAll(RequestContent.Create(
new
{
Foo = "Hello World!",
Bar = 42
}),
ContentType.ApplicationJson,
filter: ClientConnectionFilter.Create($"{group1} in groups and not({group2} in groups)"));
client.AddUserToGroup("some_group", "some_user");
// Avoid sending messages to users who do not exist.
if (client.UserExists("some_user").Value)
{
client.SendToUser("some_user", "Hi, I am glad you exist!");
}
client.RemoveUserFromGroup("some_group", "some_user");
var client = new WebPubSubServiceClient(connectionString, "some_hub");
client.RemoveConnectionFromAllGroups("some_connection");
Synchronous version:
foreach (WebPubSubGroupMember member in client.ListConnectionsInGroup("groupName"))
{
Console.WriteLine($"ConnectionId: {member.ConnectionId}, UserId: {member.UserId}");
}
Asynchronous version:
await foreach (WebPubSubGroupMember member in client.ListConnectionsInGroupAsync("groupName"))
{
Console.WriteLine($"ConnectionId: {member.ConnectionId}, UserId: {member.UserId}");
}
You can also easily enable console logging if you want to dig deeper into the requests you're making against the service.
Please take a look at the samples directory for detailed examples on how to use this library.
You can also find more samples here.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact with any additional questions or comments.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on Azure.Messaging.WebPubSub:
| Package | Downloads |
|---|---|
|
Microsoft.Azure.WebPubSub.AspNetCore
Azure SDK client library for the WebPubSub service |
|
|
Microsoft.Azure.WebJobs.Extensions.WebPubSub
Azure Functions extension for the WebPubSub service |
|
|
Blazor.Ninja.Client.Local
Blazor.Ninja Local Client Package |
|
|
Toggly.FeatureManagement
Toggly.FeatureManagement extends Microsoft.FeatureManagement with extensions to handle feature state changes, Decorate and register services that are injected when a feature is on, feature usage tracking, and integration with toggly.io |
|
|
Development.SDK.RPC
Software development kit to call RPC functions. |
Showing the top 1 popular GitHub repositories that depend on Azure.Messaging.WebPubSub:
| Repository | Stars |
|---|---|
|
microsoft/aspire
Aspire is the tool for code-first, extensible, observable dev and deploy.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 1.6.0 | 515,587 | 5/8/2025 |
| 1.5.0 | 61,079 | 4/22/2025 |
| 1.4.0 | 912,560 | 8/9/2024 |
| 1.3.0 | 1,295,454 | 11/21/2022 |
| 1.2.0 | 304,444 | 11/7/2022 |
| 1.1.0 | 5,743 | 10/29/2022 |
| 1.1.0-beta.1 | 4,047 | 8/11/2022 |
| 1.0.0 | 493,344 | 11/12/2021 |
| 1.0.0-beta.3 | 59,496 | 9/8/2021 |
| 1.0.0-beta.2 | 12,073 | 7/15/2021 |
| 1.0.0-beta.1 | 41,421 | 4/26/2021 |