TwitchLib.EventSub.Websockets
0.6.0-preview-129f195
See the version list below for details.
dotnet add package TwitchLib.EventSub.Websockets --version 0.6.0-preview-129f195
NuGet\Install-Package TwitchLib.EventSub.Websockets -Version 0.6.0-preview-129f195
<PackageReference Include="TwitchLib.EventSub.Websockets" Version="0.6.0-preview-129f195" />
paket add TwitchLib.EventSub.Websockets --version 0.6.0-preview-129f195
#r "nuget: TwitchLib.EventSub.Websockets, 0.6.0-preview-129f195"
// Install TwitchLib.EventSub.Websockets as a Cake Addin #addin nuget:?package=TwitchLib.EventSub.Websockets&version=0.6.0-preview-129f195&prerelease // Install TwitchLib.EventSub.Websockets as a Cake Tool #tool nuget:?package=TwitchLib.EventSub.Websockets&version=0.6.0-preview-129f195&prerelease
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:
- Console: https://dfederm.com/building-a-console-app-with-.net-generic-host/
- WPF: https://laurentkempe.com/2019/09/03/WPF-and-dotnet-Generic-Host-with-dotnet-Core-3-0/
You can also find a console app example for .NET 8 and for .NET Framework 4.8 in the repo.
Installation
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.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 async Task OnWebsocketConnected(object sender, WebsocketConnectedArgs e)
{
_logger.LogInformation($"Websocket {_eventSubWebsocketClient.SessionId} connected!");
if (!e.IsRequestedReconnect)
{
// subscribe to topics
}
}
private async Task 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 async Task OnWebsocketReconnected(object sender, EventArgs 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 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 examples:
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. 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 is compatible. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Logging (>= 8.0.0)
- System.Memory (>= 4.5.5)
- System.Text.Json (>= 8.0.4)
- TwitchLib.EventSub.Core (>= 2.5.3-preview-ce9df77)
-
.NETStandard 2.1
- Microsoft.Extensions.Logging (>= 8.0.0)
- System.Memory (>= 4.5.5)
- System.Text.Json (>= 8.0.4)
- TwitchLib.EventSub.Core (>= 2.5.3-preview-ce9df77)
-
net6.0
- Microsoft.Extensions.Logging (>= 8.0.0)
- System.Text.Json (>= 8.0.4)
- TwitchLib.EventSub.Core (>= 2.5.3-preview-ce9df77)
-
net7.0
- Microsoft.Extensions.Logging (>= 8.0.0)
- System.Text.Json (>= 8.0.4)
- TwitchLib.EventSub.Core (>= 2.5.3-preview-ce9df77)
-
net8.0
- Microsoft.Extensions.Logging (>= 8.0.0)
- System.Text.Json (>= 8.0.4)
- TwitchLib.EventSub.Core (>= 2.5.3-preview-ce9df77)
NuGet packages (1)
Showing the top 1 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 |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on TwitchLib.EventSub.Websockets:
Repository | Stars |
---|---|
songify-rocks/Songify
A simple tool that gets the current track from Spotify, YouTube and Nightbot.
|
Updated TwitchLib.EventSub.Core for the latest SubscriptionType changes