1

So basically what I want to do is to pull in the viewer count in real time for a stream that is currently live. I have a check to see if the stream is live or not. How do you go about pinging the API constantly to get the updated value so that I can send the updated value to the document in this case it would ultimately update a the $viewers variable that I've setup. If it makes a difference the application I'm making is using the CodeIgniter framework. Below is the code that I currently have to make the call. Right now I have to reload the page inorder to get the actual value and it's using cURL to make the API call.

class Streaming {   
    var $base_url = "https://api.twitch.tv/kraken/";
    var $client_id = 'client_id_here';
  public function load_stream_stats($channel) {
    $curl = curl_init();
    curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1,
                                    CURLOPT_URL => $this->base_url . 'streams/'. $channel .'?client_id=' . $this->client_id
                                  )
    );
    $result = curl_exec($curl);
    //makes sure that the cURL was excuted if not it generates the error stating that it didn't succeed.
    if(!curl_exec($curl)){
        die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
    } else {
        //cURL Response worked
        if(!empty($channel)) {
            $return = json_decode($result);
            if($return->stream == null) {
                return;
                // stream is offline no need to do anything
            } else {
                $stream_details = array('viewers' => $return->stream->viewers);
                // $return->stream->viewers gives a number e.g. 1052
                return $stream_details;
            }
        } 
    }
    curl_close($curl);
  }
}

The questioon in hand is how do I ping the TwitchtV API to get the updated viewer count from the REST API?

Elias Ranz
  • 401
  • 1
  • 6
  • 21
  • You need to call the script that gets the number of viewers with ajax. There's no such thing as "realtime" here. You can only poll it on an interval. You might check if the site has a limit on how many times per minute you can hit it. – Matt Mar 06 '13 at 21:28
  • I was reading that if you provide the `client_id` then there is no limit for calling the API. If I was to do this via AJAX then how would I go about writing the code to make the AJAX request to this particular function. This function is the only one that I need to have checking on an interval. I'm trying to make sure that the code is as clean as possible and easy to implement for others wanting to use my script. – Elias Ranz Mar 06 '13 at 21:33
  • Here's one example: http://stackoverflow.com/questions/9436534/ajax-tutorial-for-post-and-get – Matt Mar 06 '13 at 21:37
  • Also if it makes a difference for the request this is a library in the CodeIgniter framework, which uses the MVC approach. – Elias Ranz Mar 06 '13 at 21:37

1 Answers1

1

Working ajax is here. In the site_url/auto_update/broadcast/username I have a json value which contains the viewer count pulled from the API, so that I can auto update it. So encode the viewer count from the api and encode it into json then you can pull all the values.

<script type="text/javascript">
    var viewers = <?= $viewers ?>;
    var username = "<?= $username ?>";
    var site_url = "<?= base_url() ?>";
    var poll_interval = 3000;
</script>
<script type="text/javascript">
$(document).ready(function() {
    $('#viewers').append(viewers);

    setInterval(function(){
        $.ajax({
            url: site_url + "auto_update/broadcast/" + username,
            type: "GET",
            dataType: "json",
            cache: false,
            success: function(data) {
              if (data == null) return;
              var update_viewers = data['viewers'];
              console.log(update_viewers);
              $('#viewers').html(update_viewers);
            }
          });
    }, poll_interval);
});
</script>

The AJAX calls the function that contains the newly updated viewer count, and then encodes it into json. I call this function every 3 seconds. If the client ID is appended then you don't get rate limited. Hope this helps someone out.

If you have any questions let me know and i'll try to help out the best I can!

Elias Ranz
  • 401
  • 1
  • 6
  • 21