Managing users and groups
In the previous section, we implemented basic authentication and authorization for the SignalR server. We also updated clients to authenticate users. In this section, we will explore how to manage users and groups in SignalR. We want to add features to the chat app to enable the following:
- Allow users to know who is connected to the chat app
- Allow users to send a message to a specific user
- Allow users to join groups
- Allow users to send a message to a specific group
You can find the complete code of the sample in the chapter13/v3
folder of the GitHub repository. Let’s start with the first feature.
Managing events in SignalR
SignalR provides events to notify clients when a user connects or disconnects. We can override the OnConnectedAsync()
and OnDisconnectedAsync()
methods to handle these events. The following code shows how to override the OnConnectedAsync()
method:
public override async Task OnConnectedAsync(){ ...