@ppauel/twitch-eventsub
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

Twitch EventSub

A simple WebSocket based Twitch EventSub handler

Installation

Install Twitch EventSub with npm

  npm install @ppauel/twitch-eventsub

Usage

Example

The following imports are used in this exampe:
Client, Subscription, SubscriptionEvents

Define an array of subscriptions you want to receive.

const subscriptions = [
    new Subscription({
        broadcasterId: "your broadcaster id",
        event: SubscriptionEvents.StreamOnline
    }),
    new Subscription({
        broadcasterId: "your broadcaster id",
        event: SubscriptionEvents.StreamOffline
    })
]

Next, define your client and pass in the subscriptions array.

const client = new Client(subscriptions);

Once your client is connected, the ready event is emitted.

client.on('ready', () => {
    console.log(`Ready! Listening to ${client.subscriptions.size} subscriptions...`);
});

Once a subscribed event is fired, an event is emitted. You can build a event handler using the parameters event, eventName and broadcasterId.

client.on('event', (event, eventName, broadcasterId) => {
    switch (eventName) {
        case SubscriptionEvents.StreamOnline:
            console.log(`${event.broadcaster_user_name} is now online!`);
            break;

        case SubscriptionEvents.StreamOffline:
            console.log(`${event.broadcaster_user_name} is now offline!`);
            break;

        default:
            break;
    }
});

To log in your client, you will need your Client ID and an access token. You can find out how to generate one here: https://dev.twitch.tv/docs/authentication

client.login({ clientId: "your client id", userToken: "your access token" });

Readme

Keywords

Package Sidebar

Install

npm i @ppauel/twitch-eventsub

Weekly Downloads

1

Version

1.0.4

License

none

Unpacked Size

22.7 kB

Total Files

20

Last publish

Collaborators

  • ppauel