Socket
Socket
Sign inDemoInstall

@binaryfissiongames/twitch-webhooks

Package Overview
Dependencies
110
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @binaryfissiongames/twitch-webhooks

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


Version published
Weekly downloads
2
increased by100%
Maintainers
1
Install size
6.70 MB
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 21 Jul 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc