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

@axel669/twitch

v1.2.1

Published

Simple library for doing things with twitch chat and pubsub thats made for the browser.

Downloads

37

Readme

Axel669's Twitch Lib

Simple library for doing things with twitch chat and pubsub thats made for the browser.

Installation

yarn/npm

npm add @axel669/twitch

Browser

<script src="https://cdn.jsdelivr.net/npm/@axel669/twitch/browser/twitch.js"></script>

Usage

Client Side Bundled

import {Chat, EventSub, Multi} from "@axel669/twitch/browser"

Require

No longer supported directly.

Import

import {Chat, EventSub, Multi} from "@axel669/twitch"
//  also has a named /node export available if projects also use the browser
//  import this can help avoid some confusion
import {Chat, EventSub, Multi} from "@axel669/twitch/node"

Browser

If the cdn script tag is used

twitch.Chat(...)
twitch.EventSub(...)
twitch.Multi(...)

Events

Library uses EventBridge to manage events, see there for more details about how listeners work. All of the objects created pass the on functions from the internal bridge it uses.

Chat API

import {Chat} from "@axel669/twitch"

const chat = Chat({
    user: {
        name: "<username>",
        token: "<oauth token>",
    },
    channel: "<channel>"
})

chat.on(
    "chat.*",
    ({ data }) => console.log(data.tags.displayName, data.message)
)
chat.on(
    "bits",
    ({ data }) => console.log(data.tags.displayName, "gave", data.tags.bits)
)
chat.on(
    "sub.*",
    ({ data }) => console.log("sub", data)
)
await chat.connect()
await chat.say("message")
await chat.say("reply thread", "<msg id replied to>")
chat.disconnect()

Chat Event List

This is a list of the events that are currently parsed and sorted through. There are still some irc commands it doesn't parse (I didn't really see a reason to, but I'm also lazy), and those can be captured with the * event. Those events will always be full caps like the commands.

  • system - System messages that come through the socket (I got pretty lazy about parsing these)
  • ping - Twitch sends a ping every few minutes, the library automatically sends back the poing message expected to keep the connection alive, but you can use this event to do something custom as well
  • chat.redeem - any custom redeem that has text attached to it
  • chat.message - any chat message that comes through that isn't a sub-based event (including redeems that have a chat message attached) NOTE: highlighted message redeems come through here with the tag msgID set to "highlighted-message"
  • join/part - Sent when users are marked as joining or leaving a chat. These are not guaranteed to happen immediately but could be used to keep track of an approximate user list.
  • raid/unraid - Start or cancelled raids
  • sub.new - New subscription to the channel
  • sub.resub - Resubs
  • sub.gift - When someone is given a gift sub from a user
  • sub.gift.anon - Gift sub from anon user
  • sub.gift.mystery - This event is fired when someone gifts to the community. The individual recipients will each get a sub.gift or sub.gift.anon.
  • sub.upgrade - User continues a gifted sub
  • sub.upgrade.anon - User continues a gift sub from an anon user

Pubsub API (deprecated)

This API is deprecated in favor of the EventSub API.

const pubsub = twitch.Pubsub({
    user: {
        id: "<user id>",
        token: "<oauth token>"
    },
    topics: [
        "channel-points-channel-v1"
    ]
})
pubsub.on(
    "channel-points-channel-v1",
    ({ data }) => console.log(data.redemption.reward.title)
)
await pubsub.connect()
//  some time later
pubsub.disconnect()

EventSub API

const eventsub = twitch.EventSub({
    user: {
        id: "<user id>",
        token: "<oauth token>"
    },
    clientID: "lskadjfklsaf",
    topics: [
        "channel.follow"
    ]
})
eventsub.on(
    "channel.follow",
    ({ data }) => console.log(`${data.user_name} followed!`)
)
await eventsub.connect()
//  some time later
eventsub.disconnect()

EventSub Subscriptions

EventSub connects to Twitch's beta EventSub Websocket. Topics are the exact subscription topic that Twitch has listed for an event, and the events fired will use the same name as the subscription topic. Subscription Topics

Event List

The Pubsub will fire events based on the topics passed into the object on creation. it will fire an event with the type of the topic without any of the IDs attached. A full list of the pubsub topics and message data can be found Here.

Multi API

The Multi function takes the user, token, and topics properties from the Chat and EventSub functions and instantiates both of them internally. It fires events from both of them, and its connect and disconnect functions trigger for both internal objects. It also passes the say function from the Chat object.

Might add other connectors in the future.

const multi = twitch.Multi({
    connectors: [twitch.Chat, twitch.EventSub],
    user {
        name: "<username>",
        id: "<user id>",
        token: "<oauth token>"
    },
    channel: "<channel>",
    topics: [
        "channel.follow"
    ]
})

multi.on("chat.message", console.log)
multi.on("channel.follow", console.log)

//  returns an array of connectors .connect() results
await multi.connect()
await multi.say("message")
await multi.say("reply thread", "<msg id replied to>")
multi.disconnect()