viktorfilim
viktorfilim

Reputation: 135

Mapping SignalR Users to Connections in Groups

How can I add/remove a user from a group using the userId

Groups.Add(userid,"groupName")

that I use in my database instead of using connectionId

Groups.Add(connectionId,"groupName")

I created a mapping like here! using the User ID Provider method and I am able to do this

Clients.Users(userId).sendMessage("asa")

but

Groups.Add(userid,"groupName")

it is not working. So how can I make Groups.Add(userid,"groupName") work? Is there a special mapping that I don't know about or I am using this one wrong?

Upvotes: 3

Views: 1827

Answers (1)

Norfolc
Norfolc

Reputation: 458

It is not possible to add userId to group. You should only use connectionId. Depending of your needs you can use one another of the approaches described in the link you provided.

For example, you can add each connection to the group named by userId:

Groups.Add(Context.ConnectionId, userId);

And then you can send messages to the specified user:

Clients.Group(userId).sendMessage("asa");

Another use case may include determining userId in OnConnected method and then adding the user to needed group by its connectionId:

var groupName = GetGroupNameByUserId(userId);
Groups.Add(Context.ConnectionId, groupName);
...
Clients.Group(groupName).sendMessage("asa");

Upvotes: 2

Related Questions