0Pricing
C# Academy · Lesson

Groups, Users & Connection Management

Manage groups, target specific users or connections, and handle connect/disconnect lifecycle events.

Why Groups and Users?

In real applications you rarely want to broadcast to ALL clients. SignalR's groups let you target subsets of connections (e.g., a chat room), and users let you address all connections belonging to a specific authenticated user.

Adding Connections to Groups

Call Groups.AddToGroupAsync() with the connection ID and a group name. A connection can belong to multiple groups simultaneously.

public class ChatHub : Hub
{
    public async Task JoinRoom(string roomName)
    {
        await Groups.AddToGroupAsync(Context.ConnectionId, roomName);
        await Clients.Group(roomName)
                     .SendAsync("UserJoined", Context.User!.Identity!.Name);
    }

    public async Task LeaveRoom(string roomName)
    {
        await Groups.RemoveFromGroupAsync(Context.ConnectionId, roomName);
        await Clients.Group(roomName)
                     .SendAsync("UserLeft", Context.User!.Identity!.Name);
    }
}

All lessons in this course

  1. SignalR Hubs & Connections
  2. Groups, Users & Connection Management
  3. Strongly Typed Hubs
  4. Scaling with Redis Backplane
← Back to C# Academy