Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all group names in signalr

Tags:

c#

signalr

ok so I have this code

var c = GlobalHost.ConnectionManager.GetHubContext<SomeHubClass>().Clients;

now from this the Clients returns an IHubConext that has IHubConnectionContext that has an IGroupManager Groups. now is there anyway to get all the group names from this? Is this even possible with the signalR interface or do I have to track all the groups for each hub myself?

like image 754
dbarnes Avatar asked Oct 13 '13 23:10

dbarnes


People also ask

How many groups can SignalR handle?

So if your SignalR service can handle 40k+ users, it will handle 40k+ groups.

What are groups in SignalR?

Groups in SignalR provide a method for broadcasting messages to specified subsets of connected clients. A group can have any number of clients, and a client can be a member of any number of groups. You don't have to explicitly create groups.

Is SignalR full duplex?

SignalR supports full duplex communication that allows the client and server to transfer data back and forth.


2 Answers

SignalR does not have an exposed API for managing groups as a whole, iterating over groups, or even obtaining a summary list of groups. You can only add or remove groups. If you want to persist a list of group names, perhaps use a singleton pattern for your SomeHubClass. Persist a List<string> of group names in the singleton that you can easily access, or even a Dictionary<string, HashSet<string>> to map both the name and the hashset of connection ids, though that is probably overkill in this instance.

See http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-server#callfromoutsidehub for implementing a singleton of your hub.

like image 101
David L Avatar answered Oct 29 '22 14:10

David L


You CAN ACTUALLY GET all group names using reflection (because all fields that we need are private) like I did, also here is how I dug it: IGroupManager -> _lifetimeManager -> _groups -> _groups

IGroupManager groupManager = signalRHubContext.Groups;

object lifetimeManager = groupManager.GetType().GetRuntimeFields()
    .Single(fi => fi.Name == "_lifetimeManager")
    .GetValue(groupManager);

object groupsObject = lifetimeManager.GetType().GetRuntimeFields()
    .Single(fi => fi.Name == "_groups")
    .GetValue(lifetimeManager);

IDictionary groupsDictionary = groupsObject.GetType().GetRuntimeFields()
    .Single(fi => fi.Name == "_groups")
    .GetValue(groupsObject) as IDictionary;

List<string> groupNames = groupsDictionary.Keys.Cast<string>().ToList();
like image 32
Ayrum Avatar answered Oct 29 '22 15:10

Ayrum