This sample shows how to use Microsoft.Azure.SignalR.Management to publish messages to SignalR clients that connect to Azure SignalR Service.
dotnet add package Microsoft.Azure.SignalR.Management -v 1.0.0-*
The IServiceManager
is able to manage your Azure SignalR Service from your connection string.
var serviceManager = new ServiceManagerBuilder()
.WithOptions(option =>
{
option.ConnectionString = "<Your Connection String>";
})
.Build();
The IServiceHubContext
is used to publish messages to a specific hub.
var hubContext = await serviceManager.CreateHubContextAsync("<Your Hub Name>");
Once you create the hubContext
, you can use it to publish messages to a given hub.
// broadcast
hubContext.Clients.All.SendAsync("<Your SignalR Client Callback>", "<Arg1>", "<Arg2>", ...);
// send to a user
hubContext.Clients.User("<User ID>").SendAsync("<Your SignalR Client Callback>", "<Arg1>", "<Arg2>", ...);
// send to users
hubContext.Clients.Users(<User ID List>).SendAsync("<Your SignalR Client Callback>", "<Arg1>", "<Arg2>", ...);
// send to a group
hubContext.Clients.Group("<Group Name>").SendAsync("<Your SignalR Client Callback>", "<Arg1>", "<Arg2>", ...);
// send to groups
hubContext.Clients.Group(<Group Name List>).SendAsync("<Your SignalR Client Callback>", "<Arg1>", "<Arg2>", ...);
// add a user to a group
hubContext.UserGroups.AddToGroupAsync("<User ID>", "<Group Name>");
// remove a user from a group
hubContext.UserGroups.RemoveFromGroupAsync("<User ID>", "<Group Name>");
...
All features can be found here.
// Use specialized Json Web Token with userId claim
restClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tk);
// Serialize reloading message for transimisstion
string json = JsonConvert.SerializeObject(msg);
// Call reload rest api on the old service to start reloading connection
string url = "http://localhost:8080/api/v1/reload";
// Post REST API to reload connections on a specific service instance
restClient.PostAsync(url, data);
await hubContext.DisposeAsync();
The full message publisher sample can be found here. The usage of this sample can be found here.