Twitch

API

All Modules for Twitch are given access to make requests on the Users' behalf. All requests are automatically authenticated.

Here is an example of requesting the Top 5 Games from Twitch:

const response = await window.casthub.fetch({
    integration: 'twitch',
    method: 'GET',
    url: 'kraken/games/top',
    data: {
        limit: 5,
    },
});

This supports both the Kraken and Helix APIs:

Chat

All you need to open a new connection is an Identity ID, provided by the User during installation.

async mounted() {
    await super.mounted();

    const { id } = this.identity;

    // Connect to the Chat System.
    const chat = await window.casthub.chat(id);

    // And listen for new messages.
    chat.on('message', data => {
        console.log('NEW MESSAGE', data);
    });
}

If a connection to that specific Identity is already open (For example, when another Module has already opened it), you will be subscribed to events from that instance straight away and won't need to wait for a new connection to open.

PubSub

CastHub makes using Twitch PubSub easy by auto-subscribing you to any Topics you listen for. For example:

async mounted() {
    await super.mounted();

    const { id, identity } = this.identity;

    // Connect to the external WebSocket.
    const ws = await window.casthub.ws(id);

    /**
     * CastHub will detect you're listening to this Topic and will automatically
     * send the `LISTEN` packet to Twitch.
     */
    ws.on(`channel-points-channel-v1.${identity}`, data => {
        console.log('NEW EVENT', data);
    });
}

If a connection to that specific Identity is already open (For example, when another Module has already opened it), you will be subscribed to events from that instance straight away and won't need to wait for a new connection to open.

Events

Events are unchanged from the regular Twitch documented PubSub Events, apart from an additional event for follower, since there is no real "nice" way of fetching or listening to Twitch Follower events.

This event exposes raw user data for new followers, or at least the last 100 new followers in the last 5 seconds. For example:

async mounted() {
    await super.mounted();

    const { id } = this.identity;

    // Connect to the external WebSocket.
    const ws = await window.casthub.ws(id);

    ws.on('follower', data => {
        console.log(data);
    });
}

You can find the other supported Events from the Twitch documentation.