Monitoring the connection state in App Cloud

The latest version of the App Cloud SDK (v1.11) introduces a new type of event for monitoring whether the user is online or offline. It's simple to use:

$(bc).on("connectionstatechange", function(evt, data) {
    if (data.online) {
        // do something
    }
    else {
        // do something else
    }
});

Now you can take appropriate action at the moment a user's connection goes in or out. For example, say you want to show a status indicator:

$(bc).on("connectionstatechange", function(evt, data) {
    if (data.online) {
        hideStatusIndicator();
    }
    else {
        showStatusIndicator();
    }
});

Here's a tidier way of doing the same thing:

$(bc).on("connectionstatechange", function(evt, data) {
    displayStatusIndicator(data.online);
});

Sometimes it's necessary to check the connection status before performing a particular action. For this you can look to the bc.context object:

$(bc).on("init", function (evt) {
    if (bc.context.onLine) {
        loadNews();
    }
    else {
        displayCachedNews();
        showOfflineMessage();
    }
});

p.s. Get more tips and tricks (and share your own) by joining the Brightcove App Cloud discussion group on Google.