Listening to topics
First, you have to create an instance of the Twitch API client, as outlined in its own documentation.
Then, you register that instance with a new PubSubClient instance:
import { PubSubClient } from 'twitch-pubsub-client';
const pubSubClient = new PubSubClient();
const userId = await pubSubClient.registerUserListener(apiClient);
It's very easy to listen to events in any channel an API client is registered for now:
import { PubSubSubscriptionMessage } from 'twitch-pubsub-client';
const listener = await pubSubClient.onSubscription(userId, (message: PubSubSubscriptionMessage) => {
console.log(`${message.userDisplayName} just subscribed!`);
});
When you don't want to listen to these events anymore, you just remove the listener:
listener.remove();