<h3>Example for pipe track_* on cometApi.start and cometApi.stop</h3>
<div class="note">
    <ul>
        <li><a href="http://comet-server.org/doku.php/en" target="_blank">More info about CppComet</a></li>
        <li><a href="http://comet-server.org/doku.php/en:comet:cometql"  target="_blank">More info about CometQL</a></li>
        <li><a href="http://comet-server.com/wiki/doku.php/en:comet:javascript_api"  target="_blank">More info about auth JavaScript API</a></li> 
    </ul>
    
    <p>
        An example of disconnecting from the comet server when the window is lost focus. And connecting to the comet server when receiving focus.
    </p>
    <p>
       Connection and disconnection events generate events in the track_online channel for all who are subscribed to this channel.
    </p> 
</div>
<div class="root">  
    <hr>
    
    <div id="log"></div>
</div>
 
 
function log()
{
    console.log(arguments);
    $("#log").append(JSON.stringify(arguments)+"<hr>");
}

cometApi.start({dev_id:15, user_key:"", user_id:0, node:"app.comet-server.ru" });

cometApi.subscription("track_online", function(msg)
{  
  log(msg) 
});	
  
$(function() {
    $(window).focus(function() {
        document.title = "Active";
        log("onfocus") 

        cometApi.restart();   
    });

    $(window).blur(function() {
        log("onblur") 
        document.title = "not Active";  
        cometApi.stop();  
    });
});
log("start") 

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js
  2. https://comet-server.com/CometServerApi.js