Hi! I’m trying to set up an extension and sending messages from the Broadcaster Config to the Viewer Panel/Video using the twitch.ext.send
and twitch.ext.listen
functions.
I found this repo on github, configured and installed in my channel. When I click on “start giveaway” the POST api fires correctly and a message appears in chat (sendToChat
function). The part that doesn’t work is the one trying to updating the panel (or video component) part, using the ext.send
and ext.listen
functions.
Those are the interested part:
- live_config.js
twitch.onAuthorized(function (auth) {
log('onAuhorized() fired')
token = auth.token
channelId = auth.channelId
clientid = auth.clientId
log('token: ' + token + '\nchannelId: ' + channelId + '\nclientId: ' + clientid)
// Set up button handler
document.getElementById('btn').addEventListener('click', btnClick)
})
// On button click, toggle the status of the giveaway and send an extension chat message
btnClick = function () {
giveawayInProgress = giveawayInProgress ? false : true
// Send chat message to notify status of giveaway
sendToChat()
// Change UI based on status of giveaway
document.getElementById('btn').innerHTML = giveawayInProgress
? 'Stop Giveaway!'
: 'Start Giveaway!'
// Use PubSub to send status of giveaway
// Reference: https://dev.twitch.tv/docs/extensions/reference#send
twitch.send('broadcast', 'application/json', giveawayInProgress)
log('PubSub message sent, giveawayInProgress: ' + giveawayInProgress)
}
- extension.js
twitch.onAuthorized(function (auth) {
tuid = auth.userId
log("onAuhorized() fired\nUser " + tuid + " started extension");
});
// Listen for an incoming PubSub message and adjust HTML elements accordingly
// Reference: https://dev.twitch.tv/docs/extensions/reference/#listen
twitch.listen('broadcast', function (_target, _contentType, message) {
log("listen() fired, PubSub message received, giveawayInProgress: " + message);
var giveawayInProgress = (message == 'true');
elem = document.getElementById("txt");
if (giveawayInProgress == true) {
elem.classList.remove("is-warning");
elem.classList.add("is-success");
elem.innerHTML = "Congratulations! Your unique key is " + tuid;
}
else {
elem.classList.remove("is-success");
elem.classList.add("is-warning");
elem.innerHTML = "No active giveaways. Check back later!";
}
});