SignalR Join Group From the controller

When a user logs in to my site, they select from the drop-down list to which they belong. On the back of the login, when they are logged in, I would like to assign them to the correct SignalR group.

According to the documentation here , I can join it on the client side through:

contosoChatHubProxy.server.joinGroup(groupName); 

Is there a way to assign a group from a controller? I can name the hub as:

 var hub = new NotificationHub() hub.JoinGroup(selectedGroup); 

but the context in the hub method is NULL. Is this possible, or am I approaching this problem incorrectly? Thanks for any advice.

+1
source share
1 answer

You should not create a new hub; you can get the hub context and add the user to the group from external code, for example:

 var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>(); hubContext.Groups.Add(connectionId, groupName); 
+7
source