npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@binaryfissiongames/twitch-webhooks

v2.0.2

Published

Library to abstract subscribing to the documented Twitch webhook API endpoints, with persistence in mind.

Downloads

35

Readme

twitch-webhooks

Library for subscribing to Twitch webhooks.

Features

  • Persistence support
  • Rescheduling support
  • Integrates with extraneous methods of getting Auth/Refresh tokens
  • Integrates with an express application
  • Written in TypeScript

Installing

Run the command npm i @binaryfissiongames/twitch-webhooks while in your project directory.

Usage

    import {TwitchWebhookManager} from "@binaryfissiongames/twitch-webhooks";
    import express = require("express");

    const app = express();
    //Setup
    let webhookManager: TwitchWebhookManager = new TwitchWebhookManager({
        hostname: process.env.HOST_NAME, //Hostname for the server this is hosted on
        app: app, // Express application
        client_id: process.env.CLIENT_ID, // Twitch Client ID 
        base_path: 'webhooks', // base path to use on the express application for webhook endpoints
        getOAuthToken: getOAuthToken, // Async function to get OAuthToken from user ID
        refreshOAuthToken: refreshToken, // Function to refresh OAuth token from said OAuth token
        secret: 'supersecret' //If omitted, a cryptographically random secret will be generated
    });
    
    //setup message handler
    webhook.on('message', (webhookId: WebhookId, msg: WebhookPayload<any>) => {
        console.log(`Got message: ${msg.type} for webhook ${webhookId}, ${JSON.stringify(msg)}`);
    });
    
    //Set up error handler
    webhook.on('error', (e) => console.error(e))
    
    app.listen(8080).then(() => {
            //Subscribe to some endpoints
            let webhookId = await webhookManager.addUserFollowsSubscription({
                leaseSeconds: 6000, // Can be up to 864000 (default, 10 days),
                secret: 'thisiswebhookspecificsecret' //If omitted, the webhook manager secret is used.
            }, {to_id: "1002"});
            
            /* Code goes here */
            
            await webhookManager.unsubscribe(webhookId);
            //OR
            await webhookManager.unsubFromAll();
            
            //Destroy all owned resources (timers, persistence manager, renewal scheduler)
            //Note: As of writing this, this manager may still receive messages after destruction,
            //depending on the persistence manager implementation, AND if the underlying http(s) server is not closed.
            await webhookManager.destroy();
    });

Persistence

A specific persistence implementation is defined through the TwitchWebhookPersistenceManager (see persistence.ts) interface. An instance of an implementation may be provided to the TwitchWebhookManager's constructor as the persistenceManager property. In the case that NO such persistence manager is provided - a default, IN-MEMORY persistence manager is used. This means data about your webhooks will not persist between restarts, and could lead to twitch attempting to contact a down server if the server shuts down without properly unsubscribing from endpoints.

Rescheduling

Rescheduling is defined through the WebhookRenewalScheduler interface (see scheduling.ts). If no scheduler is provided during construction of the TwitchWebhookManager as the renewalScheduler property, then webhooks WILL NOT be renewed. A simple, default implementation is provided with the library, (see BasicWebhookRenewalScheduler in scheduling.ts). This implementation will attempt to renew a webhook after 85% of it's time from start to expiry has occurred. Smarter logic can be substituted by implementing the WebhookRenewalScheduler interface.

TODO:

  • More documentation