Real-time follower alerts with Twitch API integration

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?

Ditch the polling and use Twitch’s EventSub instead. I made this exact mistake early on - hitting rate limits left and right during testing because I was hammering their API every second. EventSub pushes notifications straight to your server when stuff like new follows happens. Way more efficient than constantly checking for updates. You’ll need to set up an endpoint to catch these webhooks and verify them with Twitch’s signature process. Switching to webhooks cut my server load big time and made notifications way more reliable. Just remember you need a public HTTPS endpoint for it to work.

Your recursive function will crash from stack overflow - there’s no exit condition. I learned this the hard way years ago when it killed my server after a few hours. Polling every second is also brutal on performance and you’ll hit rate limits fast. Professional tools like StreamLabs use WebSockets or Server-Sent Events on the frontend with EventSub webhooks on the backend. This pushes notifications instantly to the browser without hammering the API. If you’re sticking with polling temporarily, add proper error handling and exponential backoff.

Your polling approach will hit rate limits fast. Twitch has webhook subscriptions (EventSub) that push data to your server instead of constantly polling. Way cleaner than checking every second and won’t get throttled. You’ll need an HTTPS endpoint though.