Stream chat: Twitch and YouTube
Show the chat of a stream in your overlay: let the player name their Twitch channel or YouTube channel, and Racelink brings the messages.
Por ahora esta guía solo está en inglés.
How it works
Racelink reads only the chats that settings name. It reads them without an account: it can read any public chat and write nothing. The messages come in the stream topic, the same in every game and also while no game runs. Racelink includes the Stream Chat overlay that works this way; your own overlay can show the chat its own way.
Name the chat in a setting
Give the text setting where the player types the chat a role:
{
"key": "twitch",
"label": "Twitch channel",
"type": "text",
"maxLength": 25,
"default": "",
"role": "twitchChannel"
},
{
"key": "youtube",
"label": "YouTube channel or broadcast",
"type": "text",
"default": "",
"role": "youtubeChannel"
}twitchChannel- A Twitch channel as in twitch.tv/<channel>: letters a–z, digits and underscore, 3 to 25 characters, with or without a leading #.
youtubeChannel- A YouTube channel — @handle (with or without @), a channel link or a channel id UC… — or one broadcast: a link to it (youtube.com/watch?v=…, youtu.be/…, youtube.com/live/…).
While the overlay is in any layout (switched on or off), Racelink reads those chats. For a YouTube channel it reads the chats of the broadcasts live on it, the three newest, and picks up a new broadcast by itself; for a broadcast link, only that broadcast. Under a youtubeChannel setting Racelink lists the channel’s live broadcasts, so the player can choose one. Anything that is not a channel or a broadcast is ignored. At most 8 Twitch channels and 4 YouTube sources are read at once, across all overlays. The role belongs to a text setting only.
Pick the messages
stream.chat.messages holds the latest messages of every chat Racelink reads, oldest first: up to 50 of Twitch and 50 of each YouTube source. Another overlay may name other chats, so keep only yours. A message names its chat in platform (twitch or youtube) and channel; chatSource from the SDK turns a setting’s value into the same name — a Twitch channel in lowercase, @handle in lowercase, a channel id or a video id — or null when the value names nothing:
import { chatSource, type ChatMessage, type TelemetryTopics } from '@racelink/overlay';
const render = ({ stream }: TelemetryTopics): void => {
const twitch = chatSource('twitch', overlay.settings.twitch);
const youtube = chatSource('youtube', overlay.settings.youtube);
// Only the messages of the chats this overlay names, oldest first.
const mine = stream.chat.messages.filter((message: ChatMessage) =>
message.channel === (message.platform === 'twitch' ? twitch : youtube),
);
list.replaceChildren(...mine.slice(-10).map(renderMessage));
};import { chatSource, type ChatMessage } from '@racelink/overlay';
export function App() {
const { telemetry, settings, ready } = useOverlay();
// Reading the topic subscribes to it.
const { messages } = telemetry.stream.chat;
if (!ready) return null;
const twitch = chatSource('twitch', settings.twitch);
const youtube = chatSource('youtube', settings.youtube);
const mine = messages.filter((message: ChatMessage) =>
message.channel === (message.platform === 'twitch' ? twitch : youtube),
);
return (
<ol className="chat">
{mine.slice(-10).map((message) => (
<Message key={`${message.platform}:${message.id}`} message={message} />
))}
</ol>
);
}Ids are unique within a platform; a key that holds the platform too is unique across both.
Draw a message
displayName- The sender’s name as the platform shows it. color is the colour of the name on Twitch; null when unset and always on YouTube.
fragments- The message in order: text, or an emote with emoteUrl — a Twitch emote, a BetterTTV emote of a Twitch channel or a custom emoji of a YouTube channel. Standard emoji are text.
badges- broadcaster, moderator, vip, subscriber… on Twitch; broadcaster (the channel owner), moderator, member, verified on YouTube.
amount- A YouTube Super Chat or Super Sticker: the amount as YouTube shows it ($5.00); null for other messages.
isAction- A Twitch /me message: show its text in the colour of the name.
const renderMessage = (message: ChatMessage): HTMLLIElement => {
const item = document.createElement('li');
const name = document.createElement('b');
name.textContent = message.displayName;
if (message.color) name.style.color = message.color;
item.append(name, ' ');
if (message.amount !== null) item.append(`[${message.amount}] `);
for (const fragment of message.fragments) {
if (fragment.emoteUrl) {
const image = document.createElement('img');
image.src = fragment.emoteUrl;
image.alt = fragment.text;
item.append(image);
} else {
// Text, never HTML: the chat is written by strangers.
item.append(fragment.text);
}
}
return item;
};Say what the chat is doing
stream.chat.connected tells whether Racelink is connected to Twitch, and stream.chat.rooms the modes of each Twitch channel (slow mode, followers only…). For YouTube, stream.chat.youtube has an entry per source: its status (connecting, live, offline, notFound, error), channelName and the broadcasts whose chat Racelink reads. Use it to say why nothing shows yet:
const source = stream.chat.youtube.find((entry) => entry.source === youtube);
if (source?.status === 'offline') hint.textContent = 'Waiting for the broadcast…';In demo mode and in the layout editor the stream topic is a demo chat on the Twitch channel racelink and the YouTube source @racelink, whatever the settings say: while overlay.status is demo, draw it without filtering by the settings.