From the course: Learning SignalR with ASP.NET Core

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Add connections to groups

Add connections to groups

From the course: Learning SignalR with ASP.NET Core

Start my 1-month free trial

Add connections to groups

- [Instructor] The hub in this app currently uses Clients.All to broadcast any message that it receives. This means that all messages are seen by every connection. A better experience for this app would be to create a new group for each visitor to the site. Each visitor would then be able to speak to a support agent in their own private room. We'll use SignalR groups to make this work. Let's open up the chat hub, and in the OnConnectedAsync method that runs when a new connection is established, at the top of this method I'm going to do await Groups.AddToGroupAsync. To add a connection to a group you need to specify the connection ID and a group name as a string. The connection ID is just Context.ConnectionId for the current connection ID, and then we need to specify a group name. We don't have a group name to use quite yet. Since SignalR doesn't persist or keep track of the groups themselves, we'll need to add a service that can remember the groups that have been created. Since this…

Contents