SignalR different hubs

Vladislav Emelyanov 21 Reputation points
2022-08-08T15:49:30.197+00:00

Is it right to create hubs for each specific case?

WS: /public-hub- Home Page
events:
onRates

WS: /client/accounts-hub - Client Accounts Page
events:
onBalance
onAccount
onOperations

WS: /operator/notifications-hub - Operator Notifications Page
events:
onNotification

WS: /admin/report-hub - Admin Report Page
events:
onIncome
onQuotes

WS: /admin/accounts-hub - Admin Accounts Page
events:
onAccount

Or do I need to create one hub and many groups?

Thanks for help !

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,876 Reputation points Microsoft Vendor
    2022-08-09T02:35:48.657+00:00

    Hi @Vladislav Emelyanov ,
    I suggest you look at the instructions in the documentation:
    Hub object lifetime

    You don't instantiate the Hub class or call its methods from your own code on the server; all that is done for you by the SignalR Hubs pipeline. SignalR creates a new instance of your Hub class each time it needs to handle a Hub operation such as when a client connects, disconnects, or makes a method call to the server.

    Because instances of the Hub class are transient, you can't use them to maintain state from one method call to the next. Each time the server receives a method call from a client, a new instance of your Hub class processes the message. To maintain state through multiple connections and method calls, use some other method such as a database, or a static variable on the Hub class, or a different class that does not derive from Hub. If you persist data in memory, using a method such as a static variable on the Hub class, the data will be lost when the app domain recycles.

    https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server#hub-object-lifetime
    Multiple Hubs

    You can define multiple Hub classes in an application. When you do that, the connection is shared but groups are separate:

    https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server#multiple-hubs
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful