This is a guide for version 1.9.x and before. For guide on the latest SDK version, see Azure SignalR Service Management SDK. Please check Migration guidance to see how to migrate from version 1.9.x and below to 1.10.0.
NOTE
Azure SignalR Service only supports this SDK for ASP.NET CORE SignalR clients.
Package Name | Target Framework | NuGet | MyGet |
---|---|---|---|
Microsoft.Azure.SignalR.Management | .NET Standard 2.0 .NET Core App 3.0 .NET 5.0 |
Azure SignalR Service Management SDK helps you to manage SignalR clients through Azure SignalR Service directly such as broadcast messages. Therefore, this SDK can be but not limited to be used in serverless environments. You can use this SDK to manage SignalR clients connected to your Azure SignalR Service in any environment, such as in a console app, in an Azure function or in a web server.
Transient | Persistent | |
---|---|---|
Broadcast | ✔️ | ✔️ |
Broadcast except some clients | ✔️ | ✔️ |
Send to a client | ✔️ | ✔️ |
Send to clients | ✔️ | ✔️ |
Send to a user | ✔️ | ✔️ |
Send to users | ✔️ | ✔️ |
Send to a group | ✔️ | ✔️ |
Send to groups | ✔️ | ✔️ |
Send to a group except some clients | ✔️ | ✔️ |
Add a user to a group | ✔️ | ✔️ |
Remove a user from a group | ✔️ | ✔️ |
More details about different modes can be found here.
Build your instance of IServiceManager
from a ServiceManagerBuilder
var serviceManager = new ServiceManagerBuilder()
.WithOptions(option =>
{
option.ConnectionString = "<Your Azure SignalR Service Connection String>";
})
.Build();
In server mode, an endpoint
/<Your Hub Name>/negotiate
is exposed for negotiation by Azure SignalR Service SDK. SignalR clients will reach this endpoint and then redirect to Azure SignalR Service later.Unlike server scenario, there is no web server accepts SignalR clients in serverless scenario. To protect your connection string, you need to redirect SignalR clients from the negotiation endpoint to Azure SignalR Service instead of giving your connection string to all the SignalR clients.
The best practice is to host a negotiation endpoint and then you can use SignalR clients to connect your hub:
/<Your Hub Name>
.Read more details about the redirection at SignalR's Negotiation Protocol.
Both of endpoint and access token are useful when you want to redirect SignalR clients to your Azure SignalR Service.
You can use the instance of IServiceManager
generate the endpoint and corresponding access token for SignalR clients to connect to your Azure SignalR Service.
var clientEndpoint = serviceManager.GetClientEndpoint("<Your Hub Name>");
var accessToken = serviceManager.GenerateClientAccessToken("<Your Hub Name>", "<Your User ID>");
Suppose your hub endpoint is http://<Your Host Name>/<Your Hub Name>
, then your negotiation endpoint will be http://<Your Host Name>/<Your Hub Name>/negotiate
. Once you host the negotiation endpoint, you can use the SignalR clients to connect to your hub like this:
var connection = new HubConnectionBuilder().WithUrl("http://<Your Host Name>/<Your Hub Name>").Build();
await connection.StartAsync();
The sample on how to use Management SDK to redirect SignalR clients to Azure SignalR Service can be found here.
You can create an instance of IServiceHubContext
to publish messages or manage group membership. The sample on how to use Management SDK to publish messages to SignalR clients can be found here.
try
{
var hubcontext = await serviceManager.CreateHubContextAsync(hubName);
// Broadcast
hubContext.Clients.All.SendAsync(callbackName, obj1, obj2, ...);
// Send to user
hubContext.Clients.User(userId).SendAsync(callbackName, obj1, obj2, ...);
// Send to group
hubContext.Clients.Group(groupId).SendAsync(callbackName, obj1, obj2, ...);
// add user to group
await hubContext.UserGroups.AddToGroupAsync(userId, groupName);
// remove user from group
await hubContext.UserGroups.RemoveFromGroupAsync(userId, groupName);
}
finally
{
await hubContext.DisposeAsync();
}
For full sample on how to use Management SDK can be found here.
This SDK can communicates to Azure SignalR Service with two transport types:
- Transient: Create a Http request Azure SignalR Service for each message sent. The SDK simply wrap up Azure SignalR Service REST API in Transient mode. It is useful when you are unable to establish a WebSockets connection.
- Persistent: Create a WebSockets connection first and then sent all messages in this connection. It is useful when you send large amount of messages.
This class contains some utilities for managing SignalR services. For now, ServiceHubContext is the only utility provided. Other utilities will be added in the future.
Set options for service manager.
Parameters
configure
: A callback to configure theIServiceManager
Build service manager.
Interface for managing Azure SignalR service.
IServiceHubContext CreateHubContextAsync(string hubName, ILoggerFactory loggerFactory = null, CancellationToken cancellationToken = default)
Create service hub context to publish messages.
If in Persistent mode, IServiceManager also starts a WebSockets connection to Azure SignalR Service for each IServiceHubContext
. All messages will be sent in this connection.
Parameters
hubName
: The hub nameloggerFactory
: The logger factorycancellationToken
: Cancellation token for creating service hub context
string GenerateClientAccessToken(string hubName, string userId = null, IList<Claim> claims = null, TimeSpan? lifeTime = null)
Generate access token for client to connect to service directly.
Parameters
hubName
: The hub nameuserId
: The user IDclaims
: The claim list to be put into access tokenlifeTime
: The lifetime of the token. The default value is one hour.
Generate client endpoint for SignalR clients to connect to service directly.
Parameters
hubName
: The hub name
This interface manages SignalR clients connected to a specific hub in your Azure SignalR service and the interfaces follow the interface of IHubContext
with extended interfaces. For example, it can broadcast messages to all connections, send messages to a specific user, send messages to a specific group, add or remove a specific user from a specific group.
The properties are almost the same as .Net Core SignalR.
In this SDK, IUserGroupManager UserGroups
is newly added. It manages groups membership for users instead of connections. But the interface stays the same as IGroupManager Groups
.
IHubClients Clients
ClientProxy Client (string connectionId)
Get proxy for client with connection IDIClientProxy Group (string groupName)
Get proxy for group with group nameIClientProxy User (string userId)
Get proxy for user with user IDIClientProxy All
Get proxy for all clients
IGroupManager Groups
Get manager for groups, manage groups with connection IDTask AddToGroupAsync (string connectionId, string groupName, CancellationToken cancellationToken = null)
Add client with connection ID to some group with group NameTask RemoveFromGroupAsync (string connectionId, string groupName, CancellationToken cancellationToken = null)
Remove client with connection ID to some group with group Name
IUserGroupManager UserGroups
Get manager for groups, manage groups with user IDTask AddToGroupAsync (string userId, string groupName, CancellationToken cancellationToken = null)
Add user with user ID to some group with group NameTask RemoveFromGroupAsync (string userId, string groupName, CancellationToken cancellationToken = null)
Remove user with user ID to some group with group Name
Task DisposeAsync()
Stop connection if in the Persistent mode. And then dispose all unmanaged resources.