Polls
Polls are a great way for broadcasters to get feedback from their community. For example, a broadcaster can ask their viewers which game they should play next. Polls can provide from 2 to 5 choices for viewers to choose from. Viewers can vote once for free, but depending on how the poll is configured, viewers can spend Channel Points to vote multiple times (for the same choice or different choices).
Broadcasters can start a poll at any time but may run only one poll at a time. The broadcaster’s moderators or editors can create or manage the broadcaster’s polls.
Running polls is available to partners and affiliates only. Read more.
Creating a poll
To create a poll, send a POST request to the Create Poll endpoint. The endpoint requires a user access token with scope channel:manage:polls. This scope works for creating, ending, and getting polls.
The following POST shows a poll that asks what time the broadcaster should stream on Tuesday. The poll runs for 5 minutes.
In the above poll everyone gets a single vote. To let viewers cast more than one vote, enable voting with Channel Points. If enabled, viewers may cast as many votes as they want but each additional vote costs them Channel Points (to cast additional votes, the viewers must own Channel Points).
The following POST shows enabling Channel Points voting where each additional vote costs the viewer 200 Channel Points.
If the request is successful, the poll is displayed in the broadcaster’s chat.
The POST’s response contains the poll’s ID (see the response’s id field), which you can use to end the poll early or get the current state of the poll.
Ending a poll
The poll runs for the length of time that you specified in the duration field when you created the poll. In the above example, the poll runs for 5 minutes. If you want to end the poll early, send a PATCH request to the End Poll endpoint.
To end the poll:
- Set the id query parameter to the poll’s ID (the Create Poll response contains the ID).
- Set the status query parameter to TERMINATED or ARCHIVED. Set to TERMINATED if you want to end the poll and display the results of the poll in chat; otherwise, set to ARCHIVED to end the poll and remove it from chat. If TERMINATED, the results are displayed in chat for a limited time, afterwhich, the poll’s status changes to ARCHIVED.
The following PATCH shows how to end a poll and display the result in chat.
Getting a poll’s current state
To get a list of all polls that the broadcaster has run in the last 90 days, send a GET request to the Get Polls endpoint. The response returns the polls in descending order by when they were created (with the latest poll first). If your app only gets polls (and doesn’t create them), you’ll need an access token with the channel:read:polls scope; otherwise, if you’re also creating polls, you can use an access token with the channel:manage:polls scope.
The following shows a partial response.
To get a specific poll, use the id query parameter. You may specify a maximum of 100 IDs.
The following response shows the single poll that you requested. The poll’s status is ACTIVE which means the poll is still running, and it looks like someone spent channel points to cast an additional vote.
Get notified when a poll’s state changes
To get notified when someone votes in the broadcaster’s poll, subscribe to the Channel Poll Progress event.
You can also subscribe to the following events to get notified when the broadcaster starts or ends a poll. Use the Poll End event to display the final poll results.
For information about how to subscribe to events, see Managing Event Subscriptions.
Polling in React to get Twitch stream view counts
In our livestreaming SaaS ohmystream we are building a feature that allows users to track the number of live viewers across Twitch, Youtube, and Facebook. Basically this involves working a lot with the aforementioned companies’ APIs. The Twitch API is actually the easiest to work with since it doesn’t require you to go through an app audit. One thing that is a little weird though is that Twitch doesn’t have a websocket based endpoint that you can use to get the number of live viewers. Instead you have to use HTTP polling, which involves making API requests periodically using setInterval.
With react hooks this is a little bit tricky, but thankfully for us Dan Abramov wrote an excellent blog post on how to create a custom hook called useInterval that can be used for polling. The useInterval hook leverages setInterval and has the added benefit that you can dynamically update the delay. For example lets say the user goes to a different tab you could poll less often so as to put less strain on the server. Here is the code snippet for useInterval that Dan provides in his article.
Now once we understand this hook we need to actually use it in our app. Let me show you how we do this. What this function does is that if our livestream isActive (i.e. we are going live), we make a request to our server while passing in our twitch username and our twitch access token in the body of the request. The variable twitchViewCountTimer is the number of seconds that the function waits before being called again. In our app we currently have it set to 5 minutes or (1000 * 60 * 5).
Now that was on our frontend let’s look at the backend. On our node server we create the endpoint /api/twitch/view-count. Check the links at the bottom of this article for more information from the twitch docs about how to use their API. It would be great if twitch supported a websocket based api that we could use, but polling actually works pretty well. You can change the interval to best suit your needs so instead of calling the function every 5 minutes, you could call it every 1 minute. For the time being this approach works well for us. Hope you found this article helpful. If so give it a clap. Thanks so much.
Twitch channels status Chrome
I’m trying to make a Chrome extension to help with streaming on Twitch.
I don’t have any error in the console, nothing, just a blank popup under my extension icon.
Here is my code:
![]()
2 Answers 2
When you visit the api.twitch.tv URL you see a 400 error code which is a bad request. There is a message added to it saying your client id is not specified. You need to add that to your URL after registering.
Twitch gives a good explanation here. Check that out first, it should help you a lot.
Not totally clear on what you’re attempting to do or asking, and not to grave dig, but here’s a bit of helpful info, and keep in mind this is with the new Twitch API so differences considering the age of this post should be expected.
This was taken directly from one of my projects, so apologies if its not quite direct, just let me know if you’d want some more specific help! 🙂
![]()
-
The Overflow Blog
Related
Most asked in [javascript]
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.3.11.43304
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How can i check if a channel is live via the api?
![]()
ive been trying for a few hours now but but i cant get it to work. for me it looks like i need a token to get a token to get data from a channel.
![]()
So twitch recently changed it auth tokens you are going to need to register an app on twitch developer portal, this will give you a Client Id and a Client secret for your app but then you also need to get an Access Token to get this you will need to do a POST request to their api like so.https://id.twitch.tv/oauth2/token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=client_credentials(I recommend postman for testing apis btw https://www.postman.com)
If all you want to do is check if someone is live I don't think you need an Auth Token and in javascript I would do something like this.try <
so this request here will only give you a response if the streamer is live so you can tell if they're live by whether or not the try catch errors.
If you do need to use the auth tokens in javascript with axios I would do this
axios.defaults.headers.common['Client-ID'] = APP_TOKEN;axios.defaults.headers.common['Authorization'] = `Bearer $
And that's that.
Feel free to ask me about anything else I have been making a desktop app with the twitch api for several weeks now.