Hello World for Extensions
Twitch Extensions are programmable interactive overlays and panels, which help streamers interact with their viewers. The Twitch community can interact in new ways such as heat maps, real-time game data overlays, mini-games, music requests, and leaderboards. If you haven't seen an Extension, check out the TwitchDev channel page and view the Twitter panel Extension below the video player.
This guide will cover the fundamentals of building your first Extension. It will allow Twitch viewers to cycle through colors via a visual element added on top of the video player (we call this a component overlay). On the frontend, a user clicks a button in this component that changes the color of a circle. Instead of changing the CSS locally, it makes a call to a backend server for a new hex value (we call this an Extension Backend Service or EBS for short).
The recommended path for building this sample is with the Twitch Developer Rig. This is a native application to help with your development. The following steps will help you download, install, and set up the Rig.
Client ID into a text editor; you'll need this in a few steps.Client ID and Secret you found in the above steps.Now that you've built the app, let's take a look under the hood and explore how it works.
viewer.js) that makes a POST call to the EBS to request a color change for the circle./cycle/color endpoint.Let's dive into the frontend components. The HTML files allow this Extension to be run as any Extension type: overlay, component, or panel. In this example, we are using a component so video_component.html will be rendered.
The frontend logic is handled by viewer.js. The two core functions are handling authentication and making GET/POST requests to our EBS. On first load, twitch.onAuthorized enables the button, sets our auth token, and dispatches the GET request to retrieve the initial color.
twitch.onAuthorized(function (auth) {
// save our credentials
token = auth.token;
tuid = auth.userId;
// enable the button
$('#cycle').removeAttr('disabled');
setAuth(token);
$.ajax(requests.get);
});When the viewer presses the button, the onClick handler creates a POST request to the /color/cycle/ endpoint. On a succesful response, updateBlock() is called passing the payload which contains a new hex value. updateBlock() simply renders the new hex value using CSS.
$('#color').css('background-color', hex);
Our backend logic is contained in backend.js. Using hapi, we are able to spin up a light webserver. Hapi handles hosting our GET endpoint /color/query and POST endpoint /color/cycle. These endpoints then route to either colorCycleHandler or colorQueryHandler.
Hapi makes this mapping easy:
(async () => {
// Handle a viewer request to cycle the color.
server.route({
method: 'POST',
path: '/color/cycle',
handler: colorCycleHandler
});
// Handle a new viewer requesting the color.
server.route({
method: 'GET',
path: '/color/query',
handler: colorQueryHandler
});
colorCycleHandler and colorQueryHandler have similar logic, but the colorCycleHandler additonally produces a new hex value. First, these functions authenticate the request with verifyAndDecode. Then we use the channelColors array (indexed by channelID) to store hex values. After producing a new color, we save it back to the array and return the value to the frontend.
function colorCycleHandler (req) {
// Verify all requests.
const payload = verifyAndDecode(req.headers.authorization);
const { channel_id: channelId, opaque_user_id: opaqueUserId } = payload;
// Store the color for the channel.
let currentColor = channelColors[channelId] || initialColor;
// Rotate the color as if on a color wheel.
verboseLog(STRINGS.cyclingColor, channelId, opaqueUserId);
currentColor = color(currentColor).rotate(colorWheelRotation).hex();
// Save the new color for the channel.
channelColors[channelId] = currentColor;
return currentColor;
}No open issues yet, or sync has not completed.