This section will cover how to create a Websocket connection and subscribe to EventSub events with it.
Creating a WebSocket connection
To start a WebSocket connection we need to create a new instance of WebSocketConnection
.
import { WebSocketConnection } from ' @twitchfy/eventsub '
const connection = new WebSocketConnection ( {
clientSecret: ' clientSecret ' ,
userToken: ' myUserToken ' ,
maintainSubscriptions: false
To wait until the connection is fully operational you can either wait until the connect
method promise is resolved or listen to ConnectionReady
event.
import { WebSocketConnection, Events } from ' @twitchfy/eventsub '
const connection = new WebSocketConnection ( {
clientSecret: ' clientSecret ' ,
userToken: ' myUserToken ' ,
maintainSubscriptions: false
. then ( () => console . log ( ' Connection ready ' ));
connection . on (Events . ConnectionReady , () => {
console . log ( ' Connection ready x2 ' )
Subscribing to events
Having created a functional WebSocket connection it is time to subscribe to our first event.
This example will show how to subscribe to a StreamOnline
event.
import { WebSocketConnection, Events, SubscriptionTypes } from ' @twitchfy/eventsub '
const connection = new WebSocketConnection ( {
clientSecret: ' clientSecret ' ,
userToken: ' myUserToken ' ,
maintainSubscriptions: false
. then ( () => console . log ( ' Connection ready ' ));
connection . on (Events . ConnectionReady , async () => {
console . log ( ' Connection ready x2 ' );
await connection . subscribe ({ type: SubscriptionTypes . StreamOnline , options: {
broadcaster_user_id: ' userId '
Listening events
Now that you have created a new subscription we will listen to it and get notified whenever it is fired.
To achive this we can either use a callback using the onMessage
function in the subscription object or using the SubscriptionMessage
event.
import { WebSocketConnection, Events, SubscriptionTypes } from ' @twitchfy/eventsub '
const connection = new WebSocketConnection ( {
clientSecret: ' clientSecret ' ,
userToken: ' myUserToken ' ,
maintainSubscriptions: false
. then ( () => console . log ( ' Connection ready ' ));
connection . on (Events . ConnectionReady , async () => {
console . log ( ' Connection ready x2 ' );
const subscription = await connection . subscribe ( { type: SubscriptionTypes . StreamOnline , options: {
broadcaster_user_id: ' userId '
subscription . onMessage ( ( message ) => {
console . log ( ` ${ message . broadcaster . displayName } has started a stream at ${ message . stream . startedAt . toLocaleDateString () } ` )
import { WebSocketConnection, Events, SubscriptionTypes } from ' @twitchfy/eventsub '
const connection = new WebSocketConnection ( {
clientSecret: ' clientSecret ' ,
userToken: ' myUserToken ' ,
maintainSubscriptions: false
. then ( () => console . log ( ' Connection ready ' ));
connection . on (Events . ConnectionReady , async () => {
console . log ( ' Connection ready x2 ' );
await connection . subscribe ({ type: SubscriptionTypes . StreamOnline , options: {
broadcaster_user_id: ' userId '
connection . on (Events . SubscriptionMessage , ( message , subscription ) => {
//checks if the subscription is the desired subscription
if (subscription . checkSubscriptionType (SubscriptionTypes . StreamOnline )) console . log ( ` ${ message . broadcaster . displayName } has started a stream at ${ message . stream . startedAt . toLocaleDateString () } ` )
WebSocket Limits
Twitch limits the amount of connections and subscriptions you can create using WebSockets. The limits are listed in their official documentation . Here is a brief summary about them:
You can create up to 3 diffrent connections with the same clientId and userToken.
Each WebSocket connection can create up to 300 enabled subscriptions.
There is a max of 10 cost.
There is a limit of 3 subscriptions with the same type and options.
The limit is applied per userToken.