TwurpleAuthenticationGetting dataExamplesFAQMigration

Listening to PubSub topics

Authentication

This section assumes that you have prepared authentication with a user token.

Creating a PubSubClient instance works like all the other clients do; just pass an AuthProvider instance:

import { PubSubClient } from '@twurple/pubsub';

const pubSubClient = new PubSubClient({ authProvider });

It's very easy to listen to events for any authenticated user now:

import { PubSubSubscriptionMessage } from '@twurple/pubsub';

const handler = pubSubClient.onSubscription(userId, (message: PubSubSubscriptionMessage) => {
	console.log(`${message.userDisplayName} just subscribed!`);
});

When you don't want to listen to an event anymore, you just remove the handler:

handler.remove();