anthonypliu
anthonypliu

Reputation: 12437

Iterate through groups in signalR hub class

How can I iterate through a SignalR group (hub class) Groups.Add(Context.ConnectionId, "foo");

How would I iterate through the group to see whose in it? and then possibly based on the connectionId in there return a user

Upvotes: 2

Views: 3820

Answers (2)

user685590
user685590

Reputation: 2564

Possibly implement a Dictionary when the clients conn/dis/re-connect

public static readonly ConcurrentDictionary<string, object> _connections 
       = new ConcurrentDictionary<string, object>();

public Task Connect()
{
    _connections.TryAdd(Context.ConnectionId, null);
    Groups.Add(Context.ConnectionId, "users");
    //Returns Connection count. 
    return Clients.tally(_connections.Count.ToString());
}

You can expand this to include there name or group etc , but like akoeplinger say's you have to keep track of this throughout your app.

Upvotes: 1

Alexander K&#246;plinger
Alexander K&#246;plinger

Reputation: 2517

From the SignalR docs:

Groups are not persisted on the server so applications are responsible for keeping track of what connections are in what groups so things like group count can be achieved.

So no, you can't iterate over the users in a group, you need to keep track of that yourself.

Upvotes: 3

Related Questions