Manage users and groups in SignalR | Microsoft Learn

PHOTO EMBED

Sun Jul 02 2023 18:57:56 GMT+0000 (Coordinated Universal Time)

Saved by @cameron_v_r #signalr #c#

public async Task AddToGroup(string groupName)
{
    await Groups.AddToGroupAsync(Context.ConnectionId, groupName);

    await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} has joined the group {groupName}.");
}

public async Task RemoveFromGroup(string groupName)
{
    await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);

    await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} has left the group {groupName}.");
}
content_copyCOPY

A group is a collection of connections associated with a name. Messages can be sent to all connections in a group. Groups are the recommended way to send to a connection or multiple connections because the groups are managed by the application. A connection can be a member of multiple groups. Groups are ideal for something like a chat application, where each room can be represented as a group. Connections are added to or removed from groups via the AddToGroupAsync and RemoveFromGroupAsync methods.

https://learn.microsoft.com/en-us/aspnet/core/signalr/groups?view