WebSocket
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
.
1import { WebSocketConnection } from '@twitchfy/eventsub'2
3const connection = new WebSocketConnection({4 clientId: 'clientId',5 clientSecret: 'clientSecret',6 userToken: 'myUserToken',7 maintainSubscriptions: false8})9
10connection.connect();
To wait until the connection is fully operational you can either wait until the connect
method promise is resolved or listen to ConnectionReady
event.
1import { WebSocketConnection, Events } from '@twitchfy/eventsub'2
3const connection = new WebSocketConnection({4 clientId: 'clientId',5 clientSecret: 'clientSecret',6 userToken: 'myUserToken',7 maintainSubscriptions: false8})9
10connection.connect()11.then(() => console.log('Connection ready'));12
13connection.on(Events.ConnectionReady, () => {14 console.log('Connection ready x2')15})
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.
1import { WebSocketConnection, Events, SubscriptionTypes } from '@twitchfy/eventsub'2
3const connection = new WebSocketConnection({4 clientId: 'clientId',5 clientSecret: 'clientSecret',6 userToken: 'myUserToken',7 maintainSubscriptions: false8})9
10connection.connect()11.then(() => console.log('Connection ready'));12
13connection.on(Events.ConnectionReady, async() => {14 console.log('Connection ready x2');15
16 await connection.subscribe({ type: SubscriptionTypes.StreamOnline, options: {17 broadcaster_user_id: 'userId'18 }})19})
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.
1import { WebSocketConnection, Events, SubscriptionTypes } from '@twitchfy/eventsub'2
3const connection = new WebSocketConnection({4 clientId: 'clientId',5 clientSecret: 'clientSecret',6 userToken: 'myUserToken',7 maintainSubscriptions: false8})9
10connection.connect()11.then(() => console.log('Connection ready'));12
13connection.on(Events.ConnectionReady, async() => {14 console.log('Connection ready x2');15
16 const subscription = await connection.subscribe({ type: SubscriptionTypes.StreamOnline, options: {17 broadcaster_user_id: 'userId'18 }})19
20 subscription.onMessage((message) => {21 console.log(`${message.broadcaster.displayName} has started a stream at ${message.stream.startedAt.toLocaleDateString()}`)22 })23})
1import { WebSocketConnection, Events, SubscriptionTypes } from '@twitchfy/eventsub'2
3const connection = new WebSocketConnection({4 clientId: 'clientId',5 clientSecret: 'clientSecret',6 userToken: 'myUserToken',7 maintainSubscriptions: false8})9
10connection.connect()11.then(() => console.log('Connection ready'));12
13connection.on(Events.ConnectionReady, async() => {14 console.log('Connection ready x2');15
16 await connection.subscribe({ type: SubscriptionTypes.StreamOnline, options: {17 broadcaster_user_id: 'userId'18 }})19})20
21connection.on(Events.SubscriptionMessage, (message, subscription) => {22 //checks if the subscription is the desired subscription23 if(subscription.checkSubscriptionType(SubscriptionTypes.StreamOnline)) console.log(`${message.broadcaster.displayName} has started a stream at ${message.stream.startedAt.toLocaleDateString()}`)24})
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.