Calling listen inside onAuthorized, listen callback'ed four times


#1

Consider this code:

var authed = false;
window.Twitch.ext.onAuthorized(function(auth) {
    console.log('Authorised');
    if (authed) {
        return;
    }
    authed = true;

    window.Twitch.ext.listen('broadcast', function (topic, contentType, message) {
        console.log('Recv');
        // do stuff
    });
});

Compared to

var authed = false;
window.Twitch.ext.onAuthorized(function(auth) {
    console.log('Authorised');
    if (authed) {
        return;
    }
    authed = true;
});

window.Twitch.ext.listen('broadcast', function (topic, contentType, message) {
    console.log('Recv');
    // do stuff
});

The latter code results in one “Recv” when EBS Broadcasts.
The former code results in four “Recv” when the EBS Broadcasts.

It seems that performing a listen inside onAuthorised is causing the listen callback function to be called “not one” times when a broadcast occurs.

I checked PubSub in chrome inspector there is only one broadcast frame. The Twitch helper is just calling the listen callback “not once” when setup inside a onAuthorised call…

Note: no it’s not the JWT rotating, as this is on a fresh page load, it’s not rotated yet. Also “authorised” is only console logged once so it’s not spawning four calls to listen

Console log:

Authorised
-EBS TRANSMIT-
recv
recv
recv
recv

I am making only ONE listen call


Twitch.listen doesn't trigger
Duplicated broadcast messages
Extension pubsub: duplicate messages
#2

Could possibly be related to the issue I was having as well? https://discuss.dev.twitch.tv/t/help-with-listen-and-unlisten-helpers/11997

In my example, I was using listen() inside of onContext() but noticed similar behavior.


#3

Did you get a resolution for this or have to hack it? I have encountered the same issue when trying to subscribe viewers to their PubSub whisper channels. With the following code

// Twitch function handlers
var twitch = window.Twitch.ext;
var firstTimeOnly = true;

twitch.onAuthorized((auth) => {
    console.log("Twitch: onAuthorized called");
    console.log("The channel ID is", auth.channelId);
    // console.log("The extension clientId is", auth.clientId);
    console.log("My Twitch opaque user id is", auth.userId);
    // console.log("The JWT token is", auth.token);

    if (firstTimeOnly) {
        firstTimeOnly = false;

        console.log("Subbing to pubsub whisper");
        // Listen to this viewer's private PubSub whisper channel
        twitch.listen('whisper-abc'+auth.userId, (target, type, msg) => {
            console.log("New Twitch PubSub whisper:", msg);
        });
    }

});

My console output is:

Twitch: onAuthorized called
The channel ID is 174234373
My Twitch opaque user id is #############
Subbing to pubsub whisper
*****EBS SENDS 'hello' TO WHISPER******
New Twitch PubSub whisper: hello
New Twitch PubSub whisper: hello

So the callback is subscribed twice. With that boolean lock on it.

The heck?


#4

Yeah we just didn’t put the listen directly inside the callback