Website performance issues when integrating streaming API

I’m building a Drupal site and trying to create a custom block that displays user streaming status. I added a custom field where users can input their streaming username.

Here’s my current implementation:

<?php
$start_time = microtime(true);
$username = trim(strip_tags($fields['field_username']->content));
$api_url = 'https://api.twitch.tv/kraken/streams/' . strtolower($username);
$response = json_decode(file_get_contents($api_url), true);
$status_text = "Currently Offline";
$viewer_info = "Not streaming";
$category = strip_tags($fields['field_category']->content);

if ($response['stream'] !== NULL) {
    $display_name = $response['stream']['channel']['display_name'];
    $stream_description = $response['stream']['channel']['status'];
    $current_category = $response['stream']['channel']['game'];
    $viewer_info = $response['stream']['viewers'] . " watching now";
    $status_text = "Live Now";
}

$end_time = microtime(true);
$total_time = $end_time - $start_time;
$milliseconds = $total_time * 1000;
?>

<div class="<?php echo $category; ?> stream-status" title="<?php echo $viewer_info; ?>">
    <?php
        echo $milliseconds;
        print $fields['name']->content;
        echo $status_text;
    ?>
</div>

The code works but my website loads extremely slowly now. Is this a problem with my implementation or is the streaming API just naturally slow? What are some better approaches to handle this?

yep, file_get_contents is a real load killer. twitch’s api can be slow too - try using ajax or cURL to fetch the data after the page has loaded. you’ll notice a big boost in site speed!

That performance hit is from your synchronous API calls during page load. Every visitor triggers your server to wait for Twitch’s response before rendering anything else. I hit the same problem on a gaming site and fixed it with Drupal’s cache API. Cache the stream status for 2-3 minutes and only hit the API when it expires. Also, switch to Twitch’s Helix API - Kraken’s deprecated and slower. Quick fix: wrap your API call in try-catch with a short timeout so hanging requests don’t stall your pages.

drupal’s built-in queue workers are perfect for this. just create a queue item for each user and batch the api calls during cron runs instead of handling them on live requests.

Skip the real-time API calls and use a background job system instead. I hit the same wall with external APIs and switched to a cron job that updates stream statuses every few minutes in the database. Page loads stay fast since you’re just pulling cached data from your own tables. Set up a simple queue with Drupal’s Queue API or even a basic MySQL table to track when each user’s status was last updated. Yeah, your data’s slightly stale, but the performance boost is huge - I went from 4-5 second loads down to under 500ms.

Blocking API calls during page render kills performance every time. Hit this exact problem building a dashboard that pulled from multiple external APIs.

It’s not just about caching or background jobs. You need proper orchestration. What happens when Twitch goes down? Rate limits kick in? You’ve got 100 users with streaming blocks?

I fixed this with automated workflows that handle API complexity outside the web request cycle. System polls streaming APIs on schedule, handles failures gracefully, respects rate limits, updates your database automatically. Your Drupal site just reads local data.

You can trigger updates when users change streaming usernames, set different polling frequencies for active vs inactive streamers, even add webhook support for real-time updates.

Set it up once and it handles everything automatically. No more slow page loads, no more API timeouts breaking your site.