...

/

Solution Review: Adding a Secure Chat Channel

Solution Review: Adding a Secure Chat Channel

Review the solution of the "Adding a Secure Chat Channel" challenge.

We'll cover the following...

Overview

The complete solution can be found in the following playground:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;

namespace DemoApp;

[Authorize]
public class ChatHub : Hub
{
    public async Task RegisterUser(string userName)
    {
        await Groups.AddToGroupAsync(Context.ConnectionId, userName);
        await Clients.Caller.SendAsync("ReceiveMessage", $"User {userName} successfully registered");
    }

    public async Task BroadcastMessage(string userName, string message)
    {
        message = $"{userName} said: {message}";
        await Clients.All.SendAsync("ReceiveMessage", message);
    }

    public async Task SendMessageToUser(string userName, string recepientName, string message)
    {
        message = $"{userName} said: {message}";
        await Clients.Group(recepientName).SendAsync("ReceiveMessage", message);
        await Clients.Caller.SendAsync("ReceiveMessage", $"{recepientName} received the following message: {message}");
    }
}
Final setup with a secure chat hub

Solving the challenge

...