Twitch Api Check If Stream Is Live, How To Just Check If A Channel Is Live

Hi guys, I’m currently using the older API to show a status bar on my site when our stream is live.

Đang xem: Twitch api check if stream is live

Currently just doing this:https://api.twitch.tv/kraken/streams?client_id=”+twitch_key+”&channel=”+channel

There I would just check if it has proper data, if it is then we’re online.

I’m looking over the new API and I’m so confused it’s unreal.

All I want is a bar to show if we’re live on our site, how would I do that?

*

Twitch wtbblue.comelopers – 12 Jul 18

*

Reference

Twitch wtbblue.comeloper tools and services to integrate Twitch into your wtbblue.comelopment or create interactive experience on twitch.tv.

I don’t think you quite get what I’m saying.

I’m used to using the old API where we didn’t need to do any kind of auth or anything, I simply fired off the request like this:https://api.twitch.tv/kraken/streams?client_id=”+twitch_key+”&channel=”+channel

Then I get data back.

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.

Dist March 12, 2018, 1:21pm #4

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.

liamdawe March 12, 2018, 1:27pm #5

Doesn’t seem to work as simply as that.

Switching to helix, so doing:

https://api.twitch.tv/helix/streams?client_id=id&channel=name (where id is the ID from my Client ID in the wtbblue.com dashboard for my “app”)

Always gives me:

{“error”:“Unauthorized”,“status”:401,“message”:“Must provide a valid Client-ID or OAuth token”}

Dist March 12, 2018, 1:32pm #6

*

Dist:

the client id has to be in the header of the request now

You have the client id in the querystring. You need to send it as the Client-ID header.

1 Like
liamdawe March 12, 2018, 2:22pm #7

Well then, that makes more sense, I’m now able to actually get some data back thanks.

Next issue, we only seem to get the game id now not the actual name, is there a way to grab the game name?

Dist March 12, 2018, 2:26pm #8

You can use the games endpoint to get the name of the game using the id https://wtbblue.com.twitch.tv/docs/api/reference#get-games

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.

liamdawe March 12, 2018, 2:32pm #9

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.

Read more: Buy Wow Season 17 Pvp Gear Question : Wow, Pvp Season 17 To Begin Tomorrow

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.

EpsilonAlpha March 16, 2018, 2:15pm #10

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 offlineNot NULL viewers (or a number) -> Channel online

liamdawe hope that helps you

Dist March 16, 2018, 2:32pm #11

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.

EpsilonAlpha March 16, 2018, 2:37pm #12

That’s why it repeats an NULL Value

*

Or of course another option is to proof if the object exists. Which has basically the same result.

Dist March 16, 2018, 3:05pm #13

*

EpsilonAlpha:

That’s why it repeats an NULL Value

*

Or of course another option is to proof if the object exists. Which has basically the same result.

Not exactly the same.

If you the stream is offline you’ll get a response like:

{ data: <> };So if you check for viewers:

const stream = data<0>;if(stream.viewers !== null) { console.log(“Stream Online”);} else { console.log(“Stream 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.

liamdawe March 16, 2018, 2:47pm #14

In case it helps anyone else, I’m just doing this and it works perfectly.

I firstly grab the data from: https://api.twitch.tv/helix/streams?user_id=mychannel

Then, I also query for the name of the game if it exists: https://api.twitch.tv/helix/games?id=idfromtheaboverequest

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}})
EpsilonAlpha March 16, 2018, 3:18pm #15
Or like this, hope it helps

*

EpsilonAlpha March 16, 2018, 3:21pm #16

Not in my case, I use a Linux Shell Script to make calls towards the API.

Results might diff from the used program language.

Read more: How Tall Is Steve Minecraft, How Tall Is Steve From Minecraft In Real Life

system closed April 15, 2018, 3:21pm #17

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

Home Categories FAQ/Guidelines Terms of Service Privacy Policy

Powered by Discourse, best viewed with JavaScript enabled

Leave a Comment