简体   繁体   中英

Is there a built-in way to register only for specific topics?

I want to use signalR to broadcast events from my server to the client proxies. The server uses the multi tenancy architecture and I want to seperate each tenant in the signalR-Hub by the signalR groups.

Furthermore I want to broadcast my "framework events" thru signalR. The client should register himself for the events he need. Can this problem be covered by "signalR-Topics" or must the client register only the events he wants, like:

myHub.On<string>("addMessage", param => {
                Console.WriteLine(param);
            });

This sample has the restriction, that all events/methods must be defined on the server, and if the framework gets a new event, I also habe to add a new signalR method/event.

The other problem is: That if the client wants to unsuscribe from "addMessage"? Is it possible to remove the registration "myHub.On("addMessage"..." while runtime?

An other idea: That if I only publish "one" generic event-method, with an additional string parameter "topic". The clients can register on startup for specific "topics" and on the server-side it can be determined which clients get the event. I would store the client/topic mapping in a dictionary.

var topciMapping = Dictionary<string, List<string>>;
//the key is the topic name, and the list contains the signalR connectionIds

The next question will be: Can I send events to a custom list of connectionIds?

This should do the trick, right?

var connectionIds = new List<string>();
Clients.Clients(connectionIds)

If you have the list of all connectionIDs you want to send the message to, you ca basically just do this:

    foreach(var connectionId in ConnectionList) 
       Clients.Client(connectionId).sendMessage(message);

Hope this helps.

Best of luck!

The "other idea" makes absolutely sense. Note that there already is a possibility to address groups by name, which could very well suit your topics. That way you already have the users grouped that you can then easily address:

Clients.Group(groupName).addChatMessage(name, message);
Clients.OthersInGroup(groupName).addChatMessage(name, message);

Reference: Working with groups in SignalR

EDIT

The generic method is the way to go then. Also, the topic Dictionary makes sense based on what you said in the comment that you're already using groups . Also the addressing via Clients.Clients(connectionIds) should work.

So in general if you're asking if this is a decent approach, then I'd say yes. I couldn't find anything about "SignalR-Topics", wondering if I missed something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM