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_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' => '',
'app' => 'appname-live',
'url' => '_root_',
'timestamp' => time(),

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

$ch curl_init();
curl_setopt($chCURLOPT_URL""); // Do not omit trailing slash!
curl_setopt($chCURLOPT_POSTtrue); // Perform a POST
curl_setopt($chCURLOPT_RETURNTRANSFERtrue); // If not set, curl prints output to the browser
curl_setopt($chCURLOPT_HEADERfalse); // If set, curl returns headers as part of the data stream
curl_setopt($chCURLOPT_POSTFIELDS, array("json" => $request));
curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse); // Turns off verification of the SSL certificate.
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse); // Turns off verification of the SSL certificate.

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

if (!$response) { 

$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']; 

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() {
                url: '/viewer_count.php',
                dataType: 'html',
                type: 'get',
                success: function(data) {
                    setTimeout( function(){ viewer_count(); } ,20000);
function jqueryLoaded() {
        jQuery(document).ready(function() {

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


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=""></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>