TwitchLib.EventSub.Websockets 0.0.2-preview-66f945c

This is a prerelease version of TwitchLib.EventSub.Websockets.
There is a newer version of this package available.
See the version list below for details.
dotnet add package TwitchLib.EventSub.Websockets --version 0.0.2-preview-66f945c
                    
NuGet\Install-Package TwitchLib.EventSub.Websockets -Version 0.0.2-preview-66f945c
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TwitchLib.EventSub.Websockets" Version="0.0.2-preview-66f945c" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TwitchLib.EventSub.Websockets" Version="0.0.2-preview-66f945c" />
                    
Directory.Packages.props
<PackageReference Include="TwitchLib.EventSub.Websockets" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TwitchLib.EventSub.Websockets --version 0.0.2-preview-66f945c
                    
#r "nuget: TwitchLib.EventSub.Websockets, 0.0.2-preview-66f945c"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TwitchLib.EventSub.Websockets@0.0.2-preview-66f945c
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TwitchLib.EventSub.Websockets&version=0.0.2-preview-66f945c&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=TwitchLib.EventSub.Websockets&version=0.0.2-preview-66f945c&prerelease
                    
Install as a Cake Tool

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

Prerequisites

  • A project targeting .NET 6 and up (we might provide a .NET Standard 2.0 version later on)
  • Currently we only support setup via Dependency Injection

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 in the repo.

Installation

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

Setup

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

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.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;

        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.ChannelFollow += OnChannelFollow;
        }

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

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

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

            if (!e.IsRequestedReconnect)
            {
                // subscribe to topics
            }
         }

        private async void OnWebsocketDisconnected(object? sender, EventArgs 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 void OnWebsocketReconnected(object? sender, EventArgs e)
        {
            _logger.LogWarning($"Websocket {_eventSubWebsocketClient.SessionId} reconnected");
        }      
      
         private void OnErrorOccurred(object? sender, ErrorOccuredArgs e)
        {
            _logger.LogError($"Websocket {_eventSubWebsocketClient.SessionId} - Error occurred!");
        }

        private void OnChannelFollow(object? sender, ChannelFollowArgs e)
        {
            var eventData = e.Notification.Payload.Event;
            _logger.LogInformation($"{eventData.UserName} followed {eventData.BroadcasterUserName} at {eventData.FollowedAt}");
        }
    }
}

Alternatively you can also just clone the example → https://github.com/TwitchLib/TwitchLib.EventSub.Websockets/tree/master/TwitchLib.EventSub.Websockets.Example

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on TwitchLib.EventSub.Websockets:

Package Downloads
TwitchLib

Twitch C# library for accessing Twitch chat and whispers with events, Twitch API wrapper with every available API endpoint, PubSub wrapper and an Eventsub Websocket implementation

Strem.Twitch

Package Description

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on TwitchLib.EventSub.Websockets:

Repository Stars
baffler/Transparent-Twitch-Chat-Overlay
Twitch chat on top of windowed games for single monitor streamers
songify-rocks/Songify
A simple tool that gets the current track from Spotify, YouTube and Nightbot.
Version Downloads Last Updated
0.8.0 196 11/22/2025
0.8.0-preview.87.3ff8932 225 11/13/2025
0.8.0-preview.86.edf4f1c 216 11/13/2025
0.8.0-preview.85.9f998d3 223 11/12/2025
0.7.0 1,279 9/19/2025
0.7.0-preview.80.7583c0f 142 9/10/2025
0.7.0-preview.79.47f9402 170 8/24/2025
0.7.0-preview.78.d5d3fa8 85 8/16/2025
0.7.0-preview.77.43b8abc 66 8/15/2025
0.7.0-preview.76.e0eb3c4 486 7/24/2025
0.7.0-preview.75.9626728 475 7/24/2025
0.7.0-preview.74.00871ba 471 7/24/2025
0.7.0-preview.73.af00daa 143 7/16/2025
0.7.0-preview.72.d91d48d 127 7/14/2025
0.7.0-preview.71.3864955 142 7/6/2025
0.7.0-preview.70.cd91499 213 6/18/2025
0.6.0 2,537 6/7/2025
0.6.0-preview-f14064f 220 5/20/2025
0.6.0-preview-dbc970c 2,930 11/6/2024
0.6.0-preview-cde682a 160 11/5/2024
0.6.0-preview-9e27637 183 5/22/2025
0.6.0-preview-8dbcfb5 297 5/14/2025
0.6.0-preview-6f4cd77 341 11/16/2024
0.6.0-preview-62.534d542 121 5/30/2025
0.6.0-preview-532b5cd 281 5/14/2025
0.6.0-preview-3c5fac1 285 5/14/2025
0.6.0-preview-129f195 125 11/5/2024
0.6.0-preview.67.4401bf1 81 6/7/2025
0.6.0-preview.66.3e5313f 75 5/31/2025
0.5.0 5,696 4/11/2024
0.5.0-preview-ffc3181 339 7/31/2024
0.5.0-preview-ff1356d 187 6/12/2024
0.5.0-preview-fed1002 165 4/22/2024
0.5.0-preview-ef96f7f 146 4/11/2024
0.5.0-preview-d22efe0 178 4/11/2024
0.5.0-preview-cf9adcd 151 4/11/2024
0.5.0-preview-bcfc70a 173 5/20/2024
0.5.0-preview-bcc5801 183 6/17/2024
0.5.0-preview-abba5e7 218 6/25/2024
0.5.0-preview-a6ae325 321 10/30/2023
0.5.0-preview-72088d1 299 6/1/2023
0.5.0-preview-50e75fa 247 6/1/2023
0.5.0-preview-38d6070 222 6/1/2023
0.5.0-preview-10a6910 172 6/17/2024
0.4.0 2,542 5/9/2023
0.4.0-preview-7595d8b 219 5/9/2023
0.3.0 986 2/12/2023
0.3.0-preview-f4eee33 219 4/13/2023
0.3.0-preview-4523dce 245 5/9/2023
0.3.0-preview-34c0d87 267 2/11/2023
0.3.0-preview-13e1cef 251 2/11/2023
0.2.0 626 11/20/2022
0.2.0-preview-e561e8e 244 11/20/2022
0.2.0-preview-24aeaba 274 11/20/2022
0.1.0 475 11/8/2022
0.1.0-preview-ce4fc65 277 11/8/2022
0.1.0-preview-a65fad9 256 11/8/2022
0.1.0-preview-29bcaac 256 11/8/2022
0.1.0-preview-17203fa 261 11/8/2022
0.0.3 71,822 11/3/2022
0.0.3-preview-5d9eb3d 241 11/3/2022
0.0.2 530 11/1/2022
0.0.2-preview-d1dbff3 259 11/3/2022
0.0.2-preview-66f945c 269 11/1/2022
0.0.2-preview-1038807 264 11/3/2022

Initial Open Beta release