I’m currently creating a Twitch notification web app in PHP.
For the moment, the authentication works fine :
Client wants use the app -> redirected on Twitch website to accept asked scopes -> redirected on my app website with a “code” parameter -> “code” sent by curl request -> response containing the accessToken.
I stored accessToken and client username in $_COOKIE and $_SESSION variable (set $_COOKIE doesn’t work on all web browsers).
After that I have to check (in real-time ?) if the user has new followers. Certains web applications, as TNotifier, exists and do this very well… But I don’t know how.
In the Twitch API, we have only for the follows request the possibility to list all of these followers. I thought directly i will have to make requests, again and again (with one second of delay), and compare the new request with the last one… But i think there 's an other way to make that ?
Here is my code, but still thinking a better way exists…
$uknown=""; //to initialize my loop function.
comparaison($uknown);
function comparaison($u){
$options = array(
'http' => array(
'header' => 'Accept: application/vnd.twitchtv.v2+json',
'method' => 'GET',
)
);
$context = stream_context_create($options);
$result = file_get_contents('https://api.twitch.tv/kraken/channels/test_user1/follows', false, $context);
$decode_result = json_decode($result, true);
$follow=$decode_result['follows'][0];
$user=$follow['user'];
$last_follower=$user['display_name'];
if($last_follower != $u){
haveANewFollower($last_follower);
}
comparaison($last_follower);
}
Is it possible TNotifier use another way to check new followers ?
Currently the API only supports polling like you are doing. However, you should NOT use one second as your delay. That page is cached for five minutes, so there’s no use polling faster than that.
I finally use my polling diffrently, using ajax which call my php function to make curl request… So in my setTimeOut() function I have to use a kind of value like 3000000 for the delay ? I store new followers in a sql database and notify on my dashboard one by one, so it’s not a problem with a bigger delay.
But if I could suggest you one thing, it’s to integrate the notification in API. I’m using Paypal API for stream donation, and inside it there’s a system called IPN. So after the donator filling the form, he’s redirected to twitch, and a json message is sent to “notify_url” page (a php page which receives informations and send a code to paypal to confirm it’s not a fraud).
This json message could be made for twitch API, the user could define his own notify_url and he wll receive informations instantly. The “notify mode” will be only use by people who activate it, so there’s not much ressources asked to send it I think.
The functionality you are requesting are commonly known as webhooks and have been requested before. We have no plans at this time to implement webhooks.
Thank you for your answer. But I’m thinking about IRC part of the API… Can we know who follow or subscribe with the IRC API in real time ? I ask it because in bot website, like moobot, we canview in real time who is following and subscribe…
IRC will let you know who subscribes in real-time, but not who follows. I don’t know the details on how moobot works.
I would assume Moobot is using this channel follows from the API
GET /channels/:channel/follows
When it joins, call that and keep track of the names. Then poll again at a set interval and any new names are new subscribers. Voila.