Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple hubs to one connection to prevent god class

From here, it states that

All clients will use the same URL to establish a SignalR connection with your service ("/signalr" or your custom URL if you specified one), and that connection is used for all Hubs defined by the service.

There is no performance difference for multiple Hubs compared to defining all Hub functionality in a single class.

The reason I want to do this just because my only Hub is becoming god class, however, I can't find a way to do multiple hubs in .NET Core (while sharing one connection). I wish I could do so, then I could manage my code like how I did in Web APIs.

One possible solution may create multiple connections, but I have to manage different connections on my client side, just to prevent god class on the server code.

From here, someone states that mapping methods to external classes is a workaround. Is that the only workaround?

like image 874
Lee Song Avatar asked Dec 20 '18 09:12

Lee Song


People also ask

Can I have multiple hubs sharing one connection on my site?

You can have multiple hubs sharing one connection on your site. SignalR 2.0 was updated to handle multiple hubs over one signlar connection with no lost in performance.

How do I connect multiple hubs to SignalR?

All clients will use the same URL to establish a SignalR connection with your service ("/signalr" or your custom URL if you specified one), and that connection is used for all Hubs defined by the service. There is no performance difference for multiple Hubs compared to defining all Hub functionality in a single class.

What is the performance difference between multiple hubs and a class?

There is no performance difference for multiple Hubs compared to defining all Hub functionality in a single class. To start with Hubs read the WIKI entry for Hubs and Client Side of Hubs. There are couple of things according to the context of a multiple pages.

How do I connect a hub to another hub?

When you connect a hub to another hub, rather than to a computer, a special electrical connection is needed; one way or another, the send and receive signals from one hub have to be connected to the receive and send connectors on the other. There are three ways that this can be done: Many hubs have a special socket (port) called an uplink port.


1 Answers

Since SignalR has been integrated within ASP.NET Core, it is not possible any more to use one connection for multiple hubs:

In ASP.NET Core SignalR, the connection model has been simplified. Connections are made directly to a single hub, rather than a single connection being used to share access to multiple hubs.


As a workaround for a god class, you could make use of #region's to structure your code, if you want to use a single hub.

However, I do recommend to use different hubs for each purpose. For example: if I have a chat system, I would use a specific hub (ChatHub) for the chat. If I also have a quiz system, I would use a QuizHub, etc...

I don't really see the problem of handling multiple connections. Because there will be no performance issues. By seperating the code for each purpose, you are implementing separation of concerns (correct me if I'm wrong).

If you can, only initialize the client code (connection) on the pages where you actually use it, by dividing the SignalR client code (per hub) into its own file.

Let's take my last example: if the quiz has its own page, only load the SignalR client-side code on that page only.


Another thing you could try, is AJAX requests. Sometimes, I separate my code into different API controllers, and simply make an AJAX request to my API controller, to handle database transactions, for example.

You can also use some SignalR features inside that controller, by using IHubContext<T>.

In ASP.NET Core SignalR, you can access an instance of IHubContext via dependency injection. You can inject an instance of IHubContext into a controller, middleware, or other DI service. Use the instance to send messages to clients.

class SomeController : Controller
{
    private readonly IHubContext<MyHub> _hubContext;

    public SomeController(IHubContext<MyHub> hubContext)
    {
        _hubContext = hubContext;
    }
}

The documentation of using SignalR functions outside the hub, has more examples.

The downside is that you can't use all the nice features of SignalR, like adding connections to groups. It is possible to use the within your controller.

like image 90
Matthijs Avatar answered Nov 14 '22 23:11

Matthijs