Subscribe to Events
Handling Events with .on()
Utilizing the client.on()
method will make your life easier by:
- Automatically handling event subscriptions for the requested events
- Utilizing
[source].[name]
event shorthand for registering new event listeners - Allow you to easily handle event data for specific events or groups of events by defining inline event callbacks
Examples
Subscribe to a specific event, with callback:
// Subscribe to Twitch ChatMessage events
client.on('Twitch.ChatMessage', (data) => {
console.log('New Twitch Chat Message:', data);
});
You can subscribe to all events of a specific source with "[sourceName].*"
// Subscribe to all Twitch events
client.on('Twitch.*', (data) => {
console.log('Twitch Event Received:' data);
});
If you want to subscribe to all event sources & events from Streamer.bot, simply pass "*"
client.on('*', (data) => {
console.log('Event Received:', data);
});
Manually manage subscriptions
You can also manually define event subscriptions using the following methods.
On Connect
Define the events subscriptions you need when initializing your client.
You can subscribe to all events with "*"
or pass a configuration object:
// Creating a new client and automatically subscribing to all events from Streamer.bot
const client = new StreamerbotClient({
subscribe: '*'
});
// Creating a new client and automatically subscribing to specific events
const client = new StreamerbotClient({
subscribe: {
'Twitch': ['ChatMessage']
}
});
Subscribe Method
The client.subscribe()
method interacts directly with the Streamer.bot WebSocket API.
You can subscribe to all events with "*"
or pass a configuration object:
// Subscribe to all events from Streamer.bot
await client.subscribe('*');
// Subscribe to specific events
await client.subscribe({
'Twitch': ['ChatMessage']
});
Unsubscribe Method
Manually unsubscribe from any existing event subscriptions.
The client.unsubscribe()
method accepts the same format as the Subscribe.
// Unsubscribe to all events from Streamer.bot
await client.unsubscribe('*');
// Unsubscribe from specific events
await client.subscribe({
'Twitch': ['ChatMessage']
});