I don’t understand how to do it now in something like Javascript, just to display a bar saying we’re live. Do I have to go through some form of auth now and use webhooks and callbacks? The documents seem rather complicated to do such a simple request.
You don’t need to go through some form of auth now, or use webhooks. The main differences are that the client id has to be in the header of the request now, and if you’re looking for data on multiple channels instead of doing &channel=CHANNEL1,CHANNEL2,CHANNEL3 you would need to do &user_login=CHANNEL1&user_login=CHANNEL2&user_login=CHANNEL3 etc…
Helix also has rate limits, which means you can make 30 requests per min if you use just your client-id, but if you use an app token your limit would be 120 a min, but again that’s not a requirement, you can do it entirely without authentication on that endpoint if you choose.
Once you get the name, you’ll likely want to cache it so you wont have to make another request each time you need to translate a game ID into its name.
I’m thinking I might just do a PHP cron that curl’s the API, store the result on our server and then update it once a minute.
Then we’re only doing one single API call (well, maybe two to get the game name) and every single visitor is only then querying a local file on our site.
I also need that information for my project and since there was no definitive boolean value that told me this I build that value from the Amount of viewers a channel has:
NULL viewers -> Channel offline
Not NULL viewers (or a number) -> Channel online
It’s probably not best to to determine the online/offline status of a stream based on the value of the viewers field, because that field, along with the whole stream object itself, wont exist if the stream is offline.
A better way would be to see if a stream object exists for the channel you’re looking up. If it exists the stream is online, else it’s offline.
This will throw an error when the stream is offline because TypeError: Cannot read property 'viewers' of undefined, as there is no data[0] when the stream is offline (this is just when checking a single stream btw, there may be other stream objects if looking up multiple channels).
Sure you could catch and handle the error, but it’s bad practice when you can much more easily check if the object itself exists, and not error at all.
Then I save it to a local file (bundling in a game name if it exists), this is done on a cron once a minute so we never even touch the sides of the API limit - it also means actually loading it for users is super fast.
Then, with JS we do a simple check on the file:
var json_file = "twitchstatus.json";
$.getJSON(json_file, function(t)
{
if (t["data"].length > 0 && t["data"][0].type == 'live')
{
if (t["game_name"].length > 0)
{
// we have a game name, use it here
}
else if (t["data"][0].title.length > 0)
{
// no game name, use the stream title
}
// show a "we're live" bar here
}
})