Tutorial: Get connection state

Step by stepTips and Tricks Get connection state

[[service]] connection state

By default, [[service]] SDK manages a cache of sent and received data to manage disconnections. It is possible to force connection/disconnection with methods 'goOnline()' and 'goOffline()'. In order to know the current connection status, you can subscribe to the special path .info/connected using the on() method.

var myNs = new Webcom("[[baseUrl]]/base/<your-webcom-app>");
myNs.child(".info/connected").on("value", function(snapshot) {
  var eventDate=Date.now();
  var connected=snapshot.val();
  if (connected) {
    // Connected to [[service]] server (page loaded, goOnline call ...)
  } else {
    // Disconnected from [[service]] server (network lost, goOffline called ...)
  }
});

Warning: on application loading, connection may take some time to establish and thus the first call to the callback may pass connected=false. A short time later, as soon as the connection establishment is successfully completed, the callback is called again with connected=true.