onContext.game empty unless Streamer changes game

Hi,

I’m building a simple extension that gets the game currently played before matching it with an external API to show other info about it.

When the extension loads for the first time, it tries to get the game played from twitch.ext.onContext, but it’s always undefined.

After that, when the streamer updates the game currently played, in the stream option, the callback happens properly and I get the game name, so all good here. My issue is with the initial failure in getting the game name.

I don’t use the rig and here are relevant part of code (I think):

const [query, setQuery] = useState(() => {
    return "";
  });

useEffect( () => {
    getGamePlayed();
  }, []);

  const getGamePlayed = () => {
    const data = window.Twitch.ext.onContext(function(context) {
      setQuery(context.game);
      return context.game;
    });
  };

Thanks for your help!

You need to set your onContext top level. Not inside a function. It will fire once when it first loads but you miss that opportunity if you leave it in a sub function.

1 Like

Thanks @WLG3R, I’ve tested adding it directly in a script tag on the index.html page (I’m on local test right now, so script tags do fire properly), but no success, I get the exact same result.

Do you reckon this is the correct way of getting the game currently played, on page load?

I don’t know how much top level I can put it :confused:

<script>
      window.Twitch.ext.onContext(function(context, changed) {
        console.log(context);
        console.log(changed);
      });
    </script>

Inline script tags are not permitted in extensions. So this shouldn’t work.

This gist I wrote is handy for testing extension features.

Thanks Barry, I’ve just tried understanding what’s up with your test kit.

As some of my earlier testing showed, context.game is just an empty string (until the streamer updates the game played in the stream setting).

Any idea where to go from here? Do you have to authenticate in any way before being able to access this data? Would seem weird, as all other data get passed easily.

You might be suffering from

if you are testing as a panel.

Yep, seems to be it!

Thanks Barry, the issue is still there but at least I’m reassured that I’m not crazy.

Seems I am also suffering from this. onContext always has an empty string for game for me. At least if the stream is started with the same game as before. Such as someone playing the same game every day. Even over 30min later it was still firing back an empty string. It seemed to only return the correct game if the game was switched while live.

At least as a work around you can get the game from the API using HelixToken instead

That’s not a bad Idea. I was trying to think a way to use EventSub so my backend could just get notifications of the streamer being live and changing games and storing and then sending a pubsub /broadcast to the specific channel when it changes. but I just found out that this wasn’t working right when I was ready to submit. I had tested onContext but it was while live and just assumed the game field was correct. Then I found out it wasn’t. If they have some changes for me to make I will look into implement using the helix token to do an API call when first loading the UI. that isn’t a bad solution. It’s always nice to get the notifications automatically but it will work. Since it doesn’t seem like this issue is going to be fixed any time soon.

Thanks

Yeah I use a mix.

if I need the game I’ll grab it from helix locally.
Or my EBS already has it.

My EBS will use eventsub (if authed by the user) or slow cache/polling
And broadcast to pubsub on game change if detected.

But for the most part since I tie in data from another API it’s EBS round trips.

1 Like

Yeah I just looked it looks like a combination of
Stream Online
Stream Offline
Channel Update

would work for the EBS to handle this and broadcast it out.

It if’s a public extension make sure to be aware of the 10k sub limit.

So I have this in my config view
image

And I auto delete any cost 1 subs to keep within limit.

Oh yeah good point this would require a hook per channel to do it that way.

(I can’t reply anymore since my account here is new (yet my twitch account I linked with is +10 years old) :rofl:

I assume that dialog you have shown goes through the process to generate a user token? Like shows them the “allow access” page?

yeah anyone that doesn’t auth (or deauths) just gets my slow method of lookup instead of real time.

I cache for 5 minutes anyway. So EventSub just lets be invalidate the cache and rebuild quicker

1 Like