TwitchLib.EventSub.Websockets 0.8.0

TwitchLib.EventSub.Websockets

TwitchLib component to connect to Twitch's EventSub service via Websockets also known as EventSockets

Disclaimer

EventSub via Websockets is still in open beta. You can use it in production but Twitch may introduce breaking changes without prior notice. The same goes for this implementation until it reaches Version 1.0.0

Resources

If you need help on how to setup Dependency Injection in your Console or WPF Application you can have a look at these guides:

You can also find a console app example for .NET 8 and for .NET Framework 4.8 in the repo.

Installation

NuGet TwitchLib.EventSub.Websockets
Package Manager PM> Install-Package TwitchLib.EventSub.Websockets -Version 0.6.0
.NET CLI > dotnet add package TwitchLib.EventSub.Websockets --version 0.6.0
PackageReference <PackageReference Include="TwitchLib.EventSub.Websockets" Version="0.6.0" />
Paket CLI > paket add TwitchLib.EventSub.Websockets --version 0.6.0

Breaking Changes in Version 0.7

  • removed INotificationHandler interface
  • Removed deprecated versions of .NET.
  • All EventSub events were moved to TwitchLib.EventSub.Core Nuget Package, for better management across future EventSub transport Client libraries. That means their namespace changed from TwitchLib.EventSub.Websockets.Core.EventArgs.* to TwitchLib.EventSub.Core.EventArgs.*.
  • Like Events, all EventSub Models were moved to the TwitchLib.EventSub.Core package, (namespace changed from TwitchLib.EventSub.Websockets.Core.Models to TwitchLib.EventSub.Core.Models) but to ensure that the models can be used across projects some changes had to be made:
    • Properties from EventSubNotification<T> were moved to TwitchLibEventSubEventArgs<T>.
    • Property Notification was removed from TwitchLibEventSubEventArgs<T>.
    • Class EventSubMetadata was renamed to WebsocketEventSubMetadata.
  • WebsocketDisconnected and WebsocketReconnected now contain more specific EventArgs.

Setup

Step 1: Create a new project (Console, WPF, ASP.NET)

Step 2: Install the TwitchLib.EventSub.Websockets nuget package. (See above on how to do that)

Step 3: Step 3: Add necessary services and config to the DI Container

services.AddTwitchLibEventSubWebsockets();
services.AddHostedService<WebsocketHostedService>();

(The location of where to put this and the naming of variables might differ depending on what kind of project and general setup you have)

Step 4: Create the HostedService we just added to the DI container and connect to EventSub and listen to Events

using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using TwitchLib.Api;
using TwitchLib.Api.Core.Enums;
using TwitchLib.EventSub.Websockets.Core.EventArgs;
using TwitchLib.EventSub.Websockets.Core.EventArgs.Channel;

namespace TwitchLib.EventSub.Websockets.Test
{
    public class WebsocketHostedService : IHostedService
    {
        private readonly ILogger<WebsocketHostedService> _logger;
        private readonly EventSubWebsocketClient _eventSubWebsocketClient;
        private readonly TwitchApi _twitchApi = new();
        private string _userId;
        
        public WebsocketHostedService(ILogger<WebsocketHostedService> logger, EventSubWebsocketClient eventSubWebsocketClient)
        {
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));

            _eventSubWebsocketClient = eventSubWebsocketClient ?? throw new ArgumentNullException(nameof(eventSubWebsocketClient));
            _eventSubWebsocketClient.WebsocketConnected += OnWebsocketConnected;
            _eventSubWebsocketClient.WebsocketDisconnected += OnWebsocketDisconnected;
            _eventSubWebsocketClient.WebsocketReconnected += OnWebsocketReconnected;
            _eventSubWebsocketClient.ErrorOccurred += OnErrorOccurred;

            _eventSubWebsocketClient.ChannelChatMessage += OnChannelChatMessage; 
            // Get ClientId and ClientSecret by register an Application here: https://dev.twitch.tv/console/apps
            // https://dev.twitch.tv/docs/authentication/register-app/
            _twitchApi.Settings.ClientId = "YOUR_APP_CLIENT_ID";
            // Get Application Token with Client credentials grant flow.
            // https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#client-credentials-grant-flow
            _twitchApi.Settings.AccessToken = "YOUR_APPLICATION_ACCESS_TOKEN";

            // You need the UserID for the User/Channel you want to get Events from.
            // You can use await _api.Helix.Users.GetUsersAsync() for that.
            _userId = "USER_ID";
        }

        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await _eventSubWebsocketClient.ConnectAsync();
        }

        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await _eventSubWebsocketClient.DisconnectAsync();
        }

        private async Task OnWebsocketConnected(object sender, WebsocketConnectedArgs e)
        {
            _logger.LogInformation($"Websocket {_eventSubWebsocketClient.SessionId} connected!");

            if (!e.IsRequestedReconnect)
            {
                // subscribe to topics
                // create condition Dictionary
                // You need BOTH broadcaster and moderator values or EventSub returns an Error!
                var condition = new Dictionary<string, string> { { "broadcaster_user_id", _userId }, { "user_id", _userId} };
                // Create and send EventSubscription
                await _twitchApi.Helix.EventSub.CreateEventSubSubscriptionAsync("channel.chat.message", "1", condition, EventSubTransportMethod.Websocket, _eventSubWebsocketClient.SessionId);
                // If you want to get Events for special Events you need to additionally add the AccessToken of the ChannelOwner to the request.
                // https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/
            }
        }

        private async Task OnWebsocketDisconnected(object sender, WebsocketDisconnectedArgs e)
        {
            _logger.LogError($"Websocket {_eventSubWebsocketClient.SessionId} disconnected!");

            // Don't do this in production. You should implement a better reconnect strategy with exponential backoff
            while (!await _eventSubWebsocketClient.ReconnectAsync())
            {
                _logger.LogError("Websocket reconnect failed!");
                await Task.Delay(1000);
            }
        }

        private async Task OnWebsocketReconnected(object sender, WebsocketReconnectedArgs e)
        {
            _logger.LogWarning($"Websocket {_eventSubWebsocketClient.SessionId} reconnected");
        }

        private async Task OnErrorOccurred(object sender, ErrorOccuredArgs e)
        {
            _logger.LogError($"Websocket {_eventSubWebsocketClient.SessionId} - Error occurred!");
        }

        private async Task OnChannelChatMessage(object? sender, ChannelChatMessageArgs e)
        {
            _logger.LogInformation($"@{e.Payload.Event.ChatterUserName} #{e.Payload.Event.BroadcasterUserName}: {e.Payload.Event.Message.Text}");
        }
    }
}

Alternatively you can also just clone the examples:

No packages depend on TwitchLib.EventSub.Websockets.

removed INotificationHandler interface, Removed deprecated versions of .NET, All EventSub events were moved to `TwitchLib.EventSub.Core` Nuget Package, Like Events, all EventSub Models were moved to the `TwitchLib.EventSub.Core` package

Version Downloads Last updated
0.8.0 1 12/14/2025
0.8.0-preview.87.3ff8932 1 12/15/2025
0.8.0-preview.86.edf4f1c 1 12/15/2025
0.8.0-preview.85.9f998d3 1 12/15/2025
0.7.0 5 10/27/2025
0.7.0-preview.80.7583c0f 4 10/27/2025
0.7.0-preview.79.47f9402 5 09/09/2025
0.7.0-preview.78.d5d3fa8 5 09/09/2025
0.7.0-preview.77.43b8abc 5 09/09/2025
0.7.0-preview.76.e0eb3c4 6 07/29/2025
0.7.0-preview.75.9626728 6 07/29/2025
0.7.0-preview.74.00871ba 5 07/29/2025
0.7.0-preview.73.af00daa 6 07/29/2025
0.7.0-preview.72.d91d48d 6 07/29/2025
0.7.0-preview.71.3864955 6 07/29/2025
0.7.0-preview.70.cd91499 8 06/22/2025
0.6.0 7 06/22/2025
0.6.0-preview-f14064f 6 05/22/2025
0.6.0-preview-dbc970c 11 02/19/2025
0.6.0-preview-cde682a 10 02/19/2025
0.6.0-preview-9e27637 7 06/22/2025
0.6.0-preview-8dbcfb5 7 05/22/2025
0.6.0-preview-6f4cd77 10 02/19/2025
0.6.0-preview-62.534d542 7 06/22/2025
0.6.0-preview-532b5cd 6 05/22/2025
0.6.0-preview-3c5fac1 7 05/22/2025
0.6.0-preview-129f195 10 02/19/2025
0.6.0-preview.67.4401bf1 7 06/22/2025
0.6.0-preview.66.3e5313f 7 06/22/2025
0.5.0 10 02/19/2025
0.5.0-preview-ffc3181 10 02/19/2025
0.5.0-preview-ff1356d 10 02/19/2025
0.5.0-preview-fed1002 9 02/19/2025
0.5.0-preview-ef96f7f 10 02/19/2025
0.5.0-preview-d22efe0 10 02/19/2025
0.5.0-preview-cf9adcd 10 02/19/2025
0.5.0-preview-bcfc70a 10 02/19/2025
0.5.0-preview-bcc5801 10 02/19/2025
0.5.0-preview-abba5e7 10 02/19/2025
0.5.0-preview-a6ae325 10 02/19/2025
0.5.0-preview-72088d1 11 02/19/2025
0.5.0-preview-50e75fa 10 02/19/2025
0.5.0-preview-38d6070 11 02/19/2025
0.5.0-preview-10a6910 10 02/19/2025
0.4.0 10 02/19/2025
0.4.0-preview-7595d8b 10 02/19/2025
0.3.0 10 02/19/2025
0.3.0-preview-f4eee33 12 02/19/2025
0.3.0-preview-4523dce 9 02/19/2025
0.3.0-preview-34c0d87 9 02/19/2025
0.3.0-preview-13e1cef 10 02/19/2025
0.2.0 9 02/19/2025
0.2.0-preview-e561e8e 9 02/19/2025
0.2.0-preview-24aeaba 10 02/19/2025
0.1.0 10 02/19/2025
0.1.0-preview-ce4fc65 11 02/19/2025
0.1.0-preview-a65fad9 10 02/19/2025
0.1.0-preview-29bcaac 10 02/19/2025
0.1.0-preview-17203fa 10 02/19/2025
0.0.3 11 01/27/2025
0.0.3-preview-5d9eb3d 11 02/19/2025
0.0.2 10 02/19/2025
0.0.2-preview-d1dbff3 10 02/19/2025
0.0.2-preview-66f945c 10 02/19/2025
0.0.2-preview-1038807 10 02/19/2025