We have multiple App Services that act on the same data. Sometimes an event occurs on one instance of an App Service, which should trigger certain behaviour on the other instances of the same App Service, and also on other App Services (for example: clear a cache). My idea was to have every instance listen for events on a SignalR hub, and have the node where the event occurs broadcast a message to all the other nodes. I've been trying to use SignalR to create a HubConnection in the Startup, but I can't get it to trigger. Is this even possible?
services
.AddSignalR()
.AddAzureSignalR(connectionString);
// Create connection
var hubConnection = new HubConnectionBuilder()
.WithUrl(connectionString)
.WithAutomaticReconnect()
.Build();
hubConnection.On("mymethod", async () =>
{
// Perform some task
});
// Start listening on SignalR connection
hubConnection.StartAsync();
In order to trigger the SignalR hub I am creating a hub context off of an HttpContext and triggering a broadcast to each client:
var hubContext = (IHubContext<MySignalRHub>)HttpContext.RequestServices.GetService(typeof(IHubContext<MySignalRHub>));
await hubContext.Clients.All.SendAsync("mymethod");