Friday 21 April 2017

Access mqtt messages on arrived

ES6 and NodeJs, Trying to access mqtt message in html. I have a testmqtt.js that has testmqtt class.

var mqtt = require('mqtt');
var client ;
class TestMQTT {
    constructor(){
        var parent = this;
        this.payload = "";
        client  = mqtt.connect();
        client.on('connect', function () {
           client.subscribe("mqtt/demo")
        });

        client.on("message", function (topic, payload) {
            console.log(":::::::::::::::::::Cleint Payload",[topic, payload].join(": "));
            parent.payload = payload;
        });

        client.publish("mqtt/demo", "hello world!")

    }
}

var testInstance;
module.exports = {
    Init : function(){
        return new Promise(function (resolve,reject) {
            let testObj = eval(new TestMQTT());
            testInstance = testObj;
            resolve();
        });
    }
}

index.js where I have accessing exported modules.

var testSdk = require('./testmqtt.js');

module.exports.test_mamager = function(){
    return testSdk.Init().then(function(){
        return testSdk;
    });
}

messages.html - where are tying to access mqtt messages

TestLibrary.test_mamager().then(function(result){

    //Want to access messages here

})

am not getting how to access mqtt message in message.html.



via Noor Fathima

No comments:

Post a Comment