Monitoring the connection state in App Cloud

app cloud dev
 
Adam Mark's picture
Adam Mark on September 20, 2012

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.

Post new comment

The content of this field is kept private and will not be shown publicly.
0

Comments