src/simple-mqtt-client/client-browser.js
function connectBrowser (subscriptions, backlog, host, done_cb, secure) {

    // Associate the right port
    var websocket_uri;
    if(secure == true) {
        websocket_uri ='wss://' + host + ':1885/mqtt';
    }
    else {
        websocket_uri ='ws://' + host + ':1884/mqtt';
    }

    // Create client
    var client = new mqtt_lib.Client(websocket_uri, generateRandomClientId());
    // Register callback for connection lost
    client.onConnectionLost = function() {
        // TODO try to reconnect
    };
    // Register callback for received message
    client.onMessageArrived = function (message) {
        // Execute all the appropriate callbacks:
        // the ones specific to this channel with a single parameter (message)
        // the ones associated to a wildcard channel, with two parameters (message and channel)
        var cbs = findCallbacks(subscriptions, message.destinationName);
        if (cbs!==undefined) {
            cbs.forEach(function(cb) {
                if (Object.keys(subscriptions).indexOf(message.destinationName)!==-1)
                    cb(message.payloadString);
                else
                    cb(message.payloadString, message.destinationName);
            });
        }
    };
    // Connect
    client.connect({onSuccess: function() {
        // Execute optional done callback passing true
        if (done_cb!==undefined)
            done_cb(true);
        // Execute the backlog of operations performed while the client wasn't connected
        backlog.forEach(function(e) {
            e.op.apply(this, e.params);
        });
    },
        onFailure: function() {
            // Execute optional done callback passing false
            if (done_cb!==undefined)
                done_cb(false);
            else
                console.error('There was a problem initializing nutella.');
        }});
    return client;
}
function connectBrowser (subscriptions, backlog, host, done_cb, secure) {

    // Associate the right port
    var websocket_uri;
    if(secure == true) {
        websocket_uri ='wss://' + host + ':1885/mqtt';
    }
    else {
        websocket_uri ='ws://' + host + ':1884/mqtt';
    }

    // Create client
    var client = new mqtt_lib.Client(websocket_uri, generateRandomClientId());
    // Register callback for connection lost
    client.onConnectionLost = function() {
        // TODO try to reconnect
    };
    // Register callback for received message
    client.onMessageArrived = function (message) {
        // Execute all the appropriate callbacks:
        // the ones specific to this channel with a single parameter (message)
        // the ones associated to a wildcard channel, with two parameters (message and channel)
        var cbs = findCallbacks(subscriptions, message.destinationName);
        if (cbs!==undefined) {
            cbs.forEach(function(cb) {
                if (Object.keys(subscriptions).indexOf(message.destinationName)!==-1)
                    cb(message.payloadString);
                else
                    cb(message.payloadString, message.destinationName);
            });
        }
    };
    // Connect
    client.connect({onSuccess: function() {
        // Execute optional done callback passing true
        if (done_cb!==undefined)
            done_cb(true);
        // Execute the backlog of operations performed while the client wasn't connected
        backlog.forEach(function(e) {
            e.op.apply(this, e.params);
        });
    },
        onFailure: function() {
            // Execute optional done callback passing false
            if (done_cb!==undefined)
                done_cb(false);
            else
                console.error('There was a problem initializing nutella.');
        }});
    return client;
}
function connectBrowser ( subscriptions , backlog , host , done_cb , secure ) { // Associate the right port var websocket_uri ; if ( secure == true ) { websocket_uri = 'wss://' + host + ':1885/mqtt' ; } else { websocket_uri = 'ws://' + host + ':1884/mqtt' ; } // Create client var client = new mqtt_lib . Client ( websocket_uri , generateRandomClientId ( ) ) ; // Register callback for connection lost client . onConnectionLost = function ( ) { // TODO try to reconnect } ; // Register callback for received message client . onMessageArrived = function ( message ) { // Execute all the appropriate callbacks: // the ones specific to this channel with a single parameter (message) // the ones associated to a wildcard channel, with two parameters (message and channel) var cbs = findCallbacks ( subscriptions , message . destinationName ) ; if ( cbs !== undefined ) { cbs . forEach ( function ( cb ) { if ( Object . keys ( subscriptions ) . indexOf ( message . destinationName ) !== - 1 ) cb ( message . payloadString ) ; else cb ( message . payloadString , message . destinationName ) ; } ) ; } } ; // Connect client . connect ( { onSuccess : function ( ) { // Execute optional done callback passing true if ( done_cb !== undefined ) done_cb ( true ) ; // Execute the backlog of operations performed while the client wasn't connected backlog . forEach ( function ( e ) { e . op . apply ( this , e . params ) ; } ) ; } , onFailure : function ( ) { // Execute optional done callback passing false if ( done_cb !== undefined ) done_cb ( false ) ; else console . error ( 'There was a problem initializing nutella.' ) ; } } ) ; return client ; }
Helper function that connects the MQTT client in the browser
Helper function that connects the MQTT client in the browser