Duplicated broadcast messages

Hi folks!

I encounter a problem using extension pubsub messages with bits confirm window.

What I’m trying to achieve is to broadcast a custom message on bits transaction. I know there is a broadcast function on product catalog but it doesn’t provide enough customization and thus doesn’t suit desired look.

So the bug is the following. When viewer has not been using bits and when bits confirm window has never appeared broadcast messages works fine.

But after the confirm window was opened and viewer confirmed the transaction pubsub message listener begins to fire multiple times for one incoming message.

Moreover each confirm window opened adds one redundant fire of listener. After some debug I‘ve found out that function onControllerMessage from javascript helper begans to fire multiple times.

It’s a very strange and undocumented behavior.

Any tips from community or fixes from twitch would be highly appreciated!

1 Like

Where in your code are you registering your listener? If you’re doing it from inside onAuthorized then yeah it can potentially be called multiple times during the lifecycle of the extension. What you could do is have your function that registers the listener to check a boolean variable, and then flip it after it runs the first time so that any subsequent attempts to register duplicate listeners will not happen.

i check many times - my listener bind called only one time on page load.

I can confirm I am also experiencing this issue. My Extension has been live for months and this issue has never been noted before, however I personally started observing it as of yesterday.

For reference, here is my listener code

window.onload = function () {
    window.Twitch.ext.listen('broadcast', broadcastMessage);
};

Note - I initially had this code outside of the window.onload and the same thing was happening.

I noticed if I remove the listen from my JS file and instead register it by pasting into the brower dev tools console some time after my Extension was loaded I do not get the duplication issue.


Edit: After some testing, I believe the duplication only happens if the listen is bound before the initial onAuthorized is triggered. I tested this by having my listen outside the onAuthorized and binding it after several different timeouts (1ms, 100ms, 1000ms). The only instances I have observed the broadcast duplicating has been those in which the listen bind happens before the initial onAuthorized has been received.

I have now moved my listen bind inside the onAuthorized handler with a boolean to ensure it only happens on the first onAuthorized and am no longer observing duplicates. E.g.

var listenBound = false;
window.Twitch.ext.onAuthorized(function (auth) {
    if (listenBound === false) {
        window.Twitch.ext.listen('broadcast', broadcastMessage);
        listenBound = true;
    }
});

I also used the Wayback Machine to find older versions of the Twitch JavaScript helper file. The version from August 22nd appeared to have the same duplication and any older versions (March and earlier) seemed to have other API related issues which rendered them untestable. So, either this issue has been occurring since on or before August 22nd, or it is not directly related to the JavaScript helper file.

Of course my observations above may be a red herring entirely, but my testing thus far has seemed pretty consistent and I have in place what seems to be a reliable work around :crossed_fingers:

1 Like

I raised a similar issue in September 2017

TLDR: don’t listen in onAuthorised basically

Hi Barry this issue doesn’t appear to be related to that. I am experiencing exactly what OP is describing and I only noticed it this weekend on an Extension whose code has not changed for months. My code was not inside onAuthorized . Please check out my edit in my previous comment for more details of what scenarios seem to manifest this bug.

I’d rather not listen inside onAuthorized due to being called multiple times, but right now ensuring that my listen is bound after the initial onAuthorized is fire is the only way I can avoid this duplication issue.

Thanks, tf2casperr.
After moving listener bind into onAuthorized hook all works as documented.
Multiple messages are dissapeared.

I experienced the same issue as OP & tf2capserr today (Oct 15). Solution to move broadcast after onAuthorized initial call fixed it for me.