Twitch PubSub

How to listen for Twitch pubsub events in an Actor

Twitch user access token

Twitch PubSub requires a twitch user token with appropriate scopes.

For example, channel:read:subscriptions scope is required to receive events from channel-subscribe-events-v1.<channel ID>

App access token from client credentials does not work with pubsub.

Setup (Golang)

To setup Twitch chatbot in actor (golang).

  1. Add twitch-client package in go.mod
require (
artifactory.timeplay.com/twitch-client v1.1.0-5
)
  1. The bigscreen needs to send its twitch user ID and access token to actor.

  2. In the actor, create a pubusb client:

import (
"artifactory.timeplay.com/twitch-client"
"artifactory.timeplay.com/util/v2/pkg/log"
)
func OnTwitchChannel(channel string, bigscreenID string, sessionID string) {
userID := "733776234"
userToken := "7gzuyyfacnn0hshp4bxc9udlb9302k"
logParam := twitch.LoggingParam{
BigscreenUserID: "8f62e99f-4aed-4944-b0d7-cb1e1b27e07d",
SessionID: "123",
License: c.License,
Log: util.Log,
}
client := twitch.StartPubSubClient(userToken, nil, nil, logParam)
client.RegisterWhispersEvent(userID, func(e twitch.WhispersEvent) {
log.Infoln(e.Type)
})
client.RegisterSubscribeEvent(userID, func(e twitch.SubscribeEvent) {
log.Infoln(e.SubPlan)
})
}

Once a topic is registered, when the client receives an event, it also auto sends event to data pipeline to track events that occurs in the channel.

Twitch documentation regarding pubsub can be found at https://dev.twitch.tv/docs/pubsub

Twitch documentation is not entirely accurate and can be vauge. For example:

  • message field is always a serialized json string, not a json object as shown in example.
  • chat_moderator_actions event has no format example.
  • whispers event has thread type not mention and data_object at wrong level.

We use various 3rd party plugins to fill in the gap. We always deserialize to the format defined in twitch-client-go repo pubsub.go, then output to log. If fields are missing, it is likely mismatch between our format and live twitch data.

Setup (CSharp)

Requires

basic-rmb 1.3.0-2
actor-sdk 1.3.0-2

Twitch pubsub is embedded in the actor-sdk. There is no additional package to install.

The Twitch pubsub is a wrapper that use TwitchLib underneath. You can access the TwitchLib directly to process chats. The wrapper provides event logging for any pubsub events that occurs in the channel.

Below is sample code to start pubsub. It also setup Twitch chatbot and API access.

///// send these from bigscreen
// bigscreen user's twitch channel
var channelName = "<channel>";
// this is same as channel name, unless you have a dedicated chatbot account
var chatbotUsername = "<username>";
// bigscreen user's twitch access token, unless you're using a dedicated chatbot account
var twitchAccessToken = "<token>";
/////
// twitch client id can usually be hardcoded. You can also get it from license service
var twitchClientID = "<client_id>";
TwitchManager.Start(Util.log, channelName, chatbotUsername, twitchAccessToken, twitchClientID);
// listen for chat
TwitchManager.Client.ChatClient.OnMessageReceived += Client_OnMessageReceived;
// get user id
var user = await TwitchManager.Client.API.Helix.Users.GetUsersAsync(null, new List<string> { channelName }, twitchAccessToken);
// listen for whisper
TwitchManager.Client.PubSub.ListenToWhispers(user.Users[0].Id);
TwitchManager.Client.PubSub.OnWhisper += PubSub_OnWhisper;
// send topics once all listeners are setup
TwitchManager.Client.PubSub.SendTopics(twitchAccessToken);