Chat Realization with Singleton vs Separate SignalR Hub

Viewed 215

I was just wondering why the recommended solution for a Blazor-Server-Chat by Microsoft is initializing a Signal R Hub. Technically, all the C# Code is executed on the server, so it's also possible to realize the chat with a singleton:

public class MySingleton
{
    public event Action<string> OnBroadcast
    public void Send(string msg)
    {
        OnBroadcast.Invoke(msg);
    }
}

In the Blazor-Component I consume this singleton, subscribe to the event, and call Send(...).

Why I should realize this Chat with a separate SignalR Hub?

1 Answers

In the document you linked, Microsoft isn't recommending you use a normal SignalR Hub for a chat application, they are providing documentation on how you can use Azure SignalR Service; a paid Azure service that simplifies using SignalR across multiple server instances. With a singleton you don't need a hub, but it won't work if your web app has multiple instances behind a load balancer.

If you only have one web app instance and you will never expand to additional instances, then the singleton approach would probably work fine.

Related