I’m building a follower notification system for Twitch streamers using PHP. The OAuth flow works perfectly - users get redirected to Twitch for authorization, then back to my site with an auth code that I exchange for an access token.
I store the token and username in sessions and cookies for browser compatibility. Now I need to detect new followers in real-time, similar to apps like StreamLabs.
Currently I’m polling the followers endpoint repeatedly with a 1-second delay and comparing results, but this feels inefficient:
$previous_follower = "";
checkForNewFollowers($previous_follower);
function checkForNewFollowers($prev) {
$request_options = array(
'http' => array(
'header' => 'Accept: application/vnd.twitchtv.v3+json',
'method' => 'GET',
)
);
$ctx = stream_context_create($request_options);
$response = file_get_contents('https://api.twitch.tv/kraken/channels/streamer_name/follows', false, $ctx);
$data = json_decode($response, true);
$latest_follow = $data['follows'][0];
$follower_info = $latest_follow['user'];
$newest_follower = $follower_info['display_name'];
if($newest_follower != $prev) {
triggerNotification($newest_follower);
}
checkForNewFollowers($newest_follower);
}
The API returns follower data like this:
{
"follows": [
{
"user": {
"display_name": "new_follower_123",
"_id": 12345678,
"name": "new_follower_123",
"created_at": "2023-01-15T10:30:00Z"
}
}
]
}
Is there a more efficient approach than constant polling? How do professional notification tools handle this?