Twitch MediatR with ASP.NET Core

Picture: Twitch

Twitch MediatR with ASP.NET Core

Twitch

Twitch is the leading streaming platform for video games and Co.

Starting small, Twitch hosts monthly over 3.5 million streamers that can be watched over the shoulder while playing games, cooking or driving a car. This platform is therefore very well known, especially with the younger generations. More than 70% of the viewers, whose average age is 21 years, belong to the millennials. The average Twitch user already spends almost 2 hours a day on the platform to watch videos.

Many analysts say that Twitch and similar streaming platforms will completely replace television, especially among young people. Amazon acquired Twitch in 2014 for nearly $1 billion.

From the developer's point of view Twitch is very attractive, because Twitch has a very powerful and extensible API.

TwitchLib

TwitchLib is a very extensive library for .NET that allows you to access the Twitch API. This includes all kinds of events like streaming chat messages, new subscriber notifications, user content information and much much more. A separate connection is created for each channel that you want to monitor, which then makes .NET events available.

The disadvantage of this way is that this is rather unpleasant to implement with ASP.NET Core.

MediatR

MediatR is a very simplified Mediator implementation in .NET that allows us to easily implement the CQRS pattern through in-process messaging, which otherwise requires an extensive infrastructure. Instead of working with standard .NET Event Delegate Signatures, CQRS works with messages, to put it simply.

In order to subscribe to a message, the source - the sender - does not have to be directly known and thus enables an increased decoupling of the code. Instead, one or more handlers are registered that can process a certain message type.

Messages Types

MediatR uses two types of message types.

  • Requests: Requests usually have a return value, for example, the result of a database query. Usually there is only one request handler for a request message.
  • Notifications: this is a kind of fire-and-forget message and does not allow return value by the handler. Many different notification handlers can be registered to a notification message.

Register Handlers

MediatR is implemented in such a way that all handlers in assemblies are automatically recognized and not each handler has to be registered individually. One or more assemblies can be specified. If no assembly is specified, the current assembly is used automatically.

Assembly[] assemblies = <assemblies here>
services.AddMediatR(mediatrAssemblies);

Twitch Bot

I myself have been working with bots on different platforms like Skype, Teams - and Twitch for some time now.

Unfortunately the implementation of TwitchLib was always quite cumbersome and not very flexible, especially if you want to monitor multiple channels with ASP.NET Core.

Therefore I implemented my ASP.NET Core Bots for Twitch with MediatR some time ago to be able to offer more flexible solutions. I have now published this in the form of NuGet packages and on GitHub called Twitch.MediatR.

Twitch.MediatR

Twitch.MediatR is a wrapper for TwitchLib to make Twitch events available as MediatR notifications. Each notification contains the Twitch Channel link from TwitchLib in order to be able to react to a notification, e.g. to send a reply message to a chat message.

    private async void Client_OnMessageReceived(object sender, OnMessageReceivedArgs e)
    {
        await _eventProxy.PublishAsync(new TwitchChannelMessageNotification(this, e.ChatMessage)).ConfigureAwait(false);
    }

If a certain Notification Type is to be processed - in this example listening to chat messages - just register a NotificationHandler.

    public class TwitchChannelMessageNotificationHandler : INotificationHandler<TwitchChannelMessageNotification>
    {
        public async Task Handle(TwitchChannelMessageNotification request, CancellationToken cancellationToken = default)
        {
            string channel = request.ChatMessage.Channel;
            string response = $"Echo: {request.ChatMessage.Message}";

            await request.ChannelLink.SendMessageAsync(channel, response).ConfigureAwait(false);
        }
    }

This handler accepts the chatmessage notification and responds with a simple echo response.