Sourabh Sashank
Sourabh Sashank

Reputation: 300

ASP.NET SignalR: How to get the group name of particular caller?

Hi I am trying to create different group in SignalR and i am able to do so. Now i want to send message of a particular group in that group only, so how can i know that the caller to hub is of which group and send message in that group accordingly.

Here is my server side code:

public  Task JoinGroup(string groupName)
    {
        return Groups.Add(Context.ConnectionId, groupName);
    }

    public Task LeaveGroup(string groupName)
    {
        return  Groups.Remove(Context.ConnectionId, groupName);
    }

    public void SendToGroup(string groupName,string name, string message)
    {
        Clients.Group(groupName).addChatMessage(name, message);
    }

Any help is much appriciated.

Upvotes: 2

Views: 4768

Answers (1)

N. Taylor Mullen
N. Taylor Mullen

Reputation: 18301

In order to know which group a "Caller" is associated with you must track that information yourself.

This can be done via a static ConcurrentDictionary which maps connection ids to some sort of User object that you define. You can then add to it in OnConnected and removing from it in OnDisconnected. Therefore whenever you add a user to a group you can track that information in your own user object.

Hope this helps!

Upvotes: 5

Related Questions