Overview

SignalR Hub has the Groups property, which allows us to add clients to a group and remove clients from it. Each group is a one-to-many relationship between an arbitrary group name and a collection of connection IDs. Clients property of the Hub base class has the Group method, which allows us to specify a group name and send a message to all clients in the group.

Groups are helpful in many scenarios. We can use them to assign all clients of a specific category to a group. Alternatively, we can just associate a group with a specific user. Then, we won’t lose track of the connected client that represents the user. The username we specify will always map to the right connection ID, as long as the client is connected. Plus, an individual user may have multiple clients connected simultaneously. For example, we might be using the same social network platform in multiple tabs of our browser while simultaneously having it open in a mobile app.

So, let’s modify our hub to see how groups can be used.

Using SignalR groups inside the hub

We will start by adding the following methods to the LearningHub class of the SignalRServer project:

Get hands-on with 1200+ tech skills courses.