0

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");
Bart van der Drift
  • 1,287
  • 12
  • 30
  • And how exactly are your triggering the signalr hub? – Peter Bons Jul 15 '22 at 11:04
  • @PeterBons I've added the code that triggers the hub to the question – Bart van der Drift Jul 15 '22 at 12:10
  • 3
    IMHO SignalR is not the correct tool for this job, you should be using something like Azure Service Bus with a pub/sub setup using topics. Publish an event to the service bus from the app service instance where the event happens and the other instances in will pick up the publish event and act on it. – Thomas Schmidt Jul 15 '22 at 12:14

0 Answers0