TESjs

Streamline your Twitch EventSub integration with TESjs

Whether you prefer Webhooks or WebSockets as your transport method, TESjs has you covered. TESjs is the ideal solution for developers who want to save time and effort when integrating Twitch's EventSub system into their app.

Current Version:
WebSockets are here!

With WebSocket transport now available, TESjs also supports WebSocket transport. To give it a try, you can install any version v1.0.0 or later

Be sure to check out the updated documentation on how to use WebSockets with TESjs!

Skip the Hard Stuff

Twitch's EventSub system is an essential component of building real-time applications that engage with Twitch users. However, the process of setting up and managing EventSub can be time-consuming and frustrating. TESjs takes the hassle out of EventSub integration, allowing you to focus on building the features that matter.

Integrate Seamlessly

Already running an Express server in your app? TESjs has you covered! TESjs seamlessly integrates with your existing Express application, enabling you to quickly add EventSub functionality without any additional setup. Just pass your Express app into the TESjs config and you're already done.

Documentation

TESjs documentation provides detailed information on how to integrate EventSub into your app using our library. For in-depth details and examples on using TESjs, check out the documentation on GitHub. There you can learn about all of the features available to you in TESjs and get direct references to supplemental documentation from Twitch.

Install

Download TESjs now and take your Twitch EventSub integration to the next level!

npm install tesjs

or, in-browser

<script src="https://cdn.jsdelivr.net/gh/mitchwadair/tesjs@v1.0.0/dist/tes.min.js"></script>

Initialise the TES Instance

Node

                            
const TES = require('tesjs');

// initialize TESjs
const tes = new TES({
    identity: {
        id: YOUR_CLIENT_ID,
        secret: YOUR_CLIENT_SECRET // do not ship this in plaintext!! use environment variables so this does not get exposed
    },
    listener: {
        type: "webhook",
        baseURL: "https://example.com", // this MUST be an HTTPS endpoint on port 443
        secret: WEBHOOKS_SECRET // this should be different from your client secret and should also be using an environment variable
    }
});
                            
                        

Web Browser

                            
<script>
// TES is available globally as `TES` when importing from CDN in-browser
// initialize TESjs
const tes = new TES({
    identity: {
        id: YOUR_CLIENT_ID,
        authToken: YOUR_USER_ACCESS_TOKEN,
    },
    listener: {
        type: "websocket",
    }
});
</script>
                            
                        

Create an Event Listener

                            
// define an event handler for the 'channel.update' event
// NOTES: 
//   this handles ALL events of that type
//   events will not be fired until there is a subscription made for them
tes.on('channel.update', event => {
    console.log(`${event.broadcaster_user_name}'s new title is ${event.title}`);
});
                            
                        

Subscribe to an Event

                            
// create a new subscription for the 'channel.update' event for broadcaster '1337'
tes.subscribe('channel.update', {
    broadcaster_user_id: '1337'
}).then(() => {
    console.log('Subscription successful');
}).catch(err => {
    console.log(err);
});
                            
                        

Clean Up Revoked Subscriptions

                            
// the 'revocation' event will be fired if an subscription gets revoked by Twitch
// if there is any necessary cleanup for your app, use this event to do it
tes.on('revocation', subscriptionData => {
    console.log(`subscription with id ${subscriptionData.id} has been revoked`);
    // perform necessary cleanup here
});
                            
                        

Use External Authentication Handling

                            
const TES = require('tesjs');

// example app access token fetching
const customTokenRefresh = async () => {
    // ... token fetching business logic ...
    return yourFetchedToken;
}

// initialize TESjs
// if you are using TESjs in conjunction with other Twitch-related libraries or API usage,
// you can use the `identity.onAuthenticationFailure` configuration option to handle your
// fetching/refreshing your app access token yourself
const tes = new TES({
    identity: {
        id: YOUR_CLIENT_ID,
        secret: YOUR_CLIENT_SECRET, // do not ship this in plaintext!! use environment variables so this does not get exposed
        onAuthenticationFailure: customTokenRefresh,
        accessToken: EXISTING_ACCESS_TOKEN, // if you already have an app access token, put it here to avoid an unnecessary token refresh
    },
    listener: {
        type: "webhook",
        baseURL: "https://example.com", // this MUST be an HTTPS endpoint on port 443
        secret: WEBHOOKS_SECRET // this should be different from your client secret and should also be using an environment variable
    }
});
                            
                        

Support the Project

If you would like to support the development and maintenance of TESjs, please consider supporting the project through GitHub Sponsors or through a one-time PayPal donation . Donations are not required but are much appreciated and will directly support efforts to continue developing TESjs.

PayPal Logo GitHub Sponsors Logo