Use SignalR in MediatR Notifications

Use SignalR in MediatR Notifications

SignalR is a wonderful, easy and fast way to provide users with push messages - and is also built into ASP.NET Core 3.
In this short code sample I show you how to inform a client about SignalR based on MediatR notifications.

First you need its hub, which is necessary in SignalR for communication.
All clients connect to this hub and are supplied with corresponding events or methods.

    public class PortalHub : Hub<IPortalHubClient>
    {
         public PortalHub( ) { }
    }

So that we do not need Magic Strings, we use an interface. The hub automatically receives the corresponding stubs during the compilation process. Therefore we do not have to code the methods manually. That's nice!

    public interface IPortalHubClient
    {
        Task NewPrivateMessage(string fromUserName, string subject);
    }

We throw the MediatR notification at the appropriate places, e.g. when a new message has been added to the database. The basic idea is that a notification is as slim as possible and only contains the necessary information.

    public class UserPrivateMessageNewNotification : INotification
    {
        public int ToUserId { get; }
        public int FromUserId { get; }
        public string FromUserName { get; }
        public int MessageId { get; }
        public string MessageSubject { get; }

        public UserPrivateMessageNewNotification(
            int toUserId, 
            int fromUserId, string fromUserName, 
            int messageId, string messageSubject)
        {
            ToUserId = toUserId;
            FromUserId = fromUserId;
            FromUserName = fromUserName;
            MessageId = messageId
            MessageSubject = messageSubject;
        }
    }

Hint: By default, the MediatR pipeline awaits for all notification handlers to process; accordingly, the code that calls the notification may slow down.
There is a real Fire-and-Forget notification in MediatR, if the execution of the notifications is not waited for.

// this awaits all handlers
await _mediatr.Publish(new MyNotification()); 

// this does not wait, but executes the notification "in background"
_mediatr.Publish(new MyNotification()); 

// this does not wait, but executes the notification "in background"
Task.Run(async () => await _mediatr.Publish(new MyNotification())); 

The last point is the notification handler.
Here we process information about the new message: we inform the recipient.
For this we inject the HubContext and use the interface to call the Hub without magic strings.

    public class NotifyUserOnNewPrivateMessageNotificationHandler 
        : INotificationHandler<UserPrivateMessageAddedNotification>
    {
        // IHubContext is used to dependency injection purposes
        // IPortalHubClient is used, so we dont use magic strings
        private readonly IHubContext<PortalHub, IPortalHubClient> _portalHubContext;

        public NotifyUserOnNewPrivateMessageNotificationHandler(
            IHubContext<PortalHub, IPortalHubClient> portalHubContext)
        {
            _portalHubContext = portalHubContext;
        }

        public async Task Handle(
            UserPrivateMessageNewNotification notification, 
            CancellationToken cancellationToken)
        {
            // read relevant data from notification
            string recipientUserId = notification.ToUserId;
            string fromUsername = notification.FromUserName;
            string subject = notification.MessageSubject;

            // We use the context to avoid magic strings
            // Our Id is also our authentication 
            //   identifier, which is used by SignalR by default too
            await _portalHubContext
                        .Clients
                        .User(recipientUserId)
                        .NewPrivateMessage(fromUsername, subject);
        }
    }