Viewer Count Example

Code Examples to Query The ScaleEngine API for Current Stream Viewers

Sample PHP Code

Create the file viewer_count.php on your webserver, and use the following example code to query the API for viewers to your ScaleEngine application. Just add your API keys (from your account profile, and your CDN number.

$api_key = "00000000000000000000000000000000"; 
$api_secret = "0000000000000000000000000000000000000000000000000000000000000000"; 
$pass = md5(rand(1000,9999).microtime(true).rand(1000,9999)); //randomly generated password string

$arrAPI = array(
    'api_key' => $api_key,
    'cdn' => 1234,
    'command' => 'viewer.live',
        'app' => 'appname-live',
        'url' => '_root_',
    'timestamp' => time(),
    );

$request = json_encode($arrAPI);
$sig = base64_encode(hash_hmac('sha256', $request, $api_secret, true));
$arrAPI['signature'] = $sig;
$request = json_encode($arrAPI);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.scaleengine.net/stable/"); // Do not omit trailing slash!
curl_setopt($ch, CURLOPT_POST, true); // Perform a POST
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // If not set, curl prints output to the browser
curl_setopt($ch, CURLOPT_HEADER, false); // If set, curl returns headers as part of the data stream
curl_setopt($ch, CURLOPT_POSTFIELDS, array("json" => $request));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Turns off verification of the SSL certificate.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Turns off verification of the SSL certificate.

$response = curl_exec($ch); //Execute the API Call 

if (!$response) { 
  die('13'); 
} 

$data = json_decode("$response",true);  
//show entire response if you like
//echo var_dump(json_decode("$response"));

if ($data['status'] == "failure") {
	$num = "0";
}

if ($data['status'] == "success") {
	$num = $data['data'][0]['viewer_count_sum']; 
}
echo $num; 

Sample JQuery

Create the file viewer_count.js on your webserver, and use the following example code to query the API every 20 seconds by calling the php script above.

function viewer_count() {
	jQuery.ajax({
   		url: '/viewer_count.php',
		dataType: 'html',
		type: 'get',
		success: function(data) {
			jQuery('#viewer_count').html(data).show;
			setTimeout( function(){ viewer_count(); } ,20000);
		}
	});
}
function jqueryLoaded() {
        jQuery(document).ready(function() {
                viewer_count();
        });
}

function checkJquery() {
    if (window.jQuery) {
        jqueryLoaded();
    } else {
        window.setTimeout(checkJquery, 100);
    }
}

checkJquery();

Sample HTML

Finally, add a small div with span id that matches the ajax js function name (viewer_count) to display the returned number from the API on your website and have it check in for updates every 20 seconds. Be sure to include the jquery js file somewhere on the page.


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<div style="display: inline; width: 25px; color: #5588BB;">: <span id="viewer_count" style="text-align: center;"></span></div> <script type="text/javascript" src="/viewer_count.js"></script>