Barguast
Barguast

Reputation: 6196

SignalR - sending to a number of clients (not a group)

I'm using SignalR in a prototype I'm building. I need to broadcast messages to a number of clients, but there is some logic as to which clients will get which message which is complex enough to rule out using Groups. Instead, I'm basically checking each connected client - if they are applicable, they're added to a List<>. I then send the message using:

var clients = DetermineClients(msg);
foreach (var client in clients)
    client.Send(msg);

Of course, if I were able to use Groups, I could do...:

var group = DetermineGroup(msg);
group.Send(msg);

... since the 'Send' method of group appears to basically do the same thing - enumerate the clients in the group and call 'Send' on those. Is this the 'correct' way to do this? Or is there some way to create a temporary group on the fly? The 'dynamic' type of a group or singular client makes it difficult for me to determine whether I'm doing this right. If there is some magic going on behind the scene for optimising a broadcast to a number of clients, I'd obviously rather use that!

Any advice would be appreciated. Let me know if you need more info.

Upvotes: 2

Views: 457

Answers (1)

N. Taylor Mullen
N. Taylor Mullen

Reputation: 18311

If you're simply trying to send the same message to a number of different clients based on a complex criteria your best bet would be to have a group that contains all of your clients and then query to find out which clients DO NOT fit your criteria. With the list of connection IDs that do not fit your criteria you can do:

Clients.Group("myGroupThatHasAllMyClients",myExcludedConnectionIds).bar();

Upvotes: 3

Related Questions