Search code examples
node.jsmqttmosca

nodejs - why mosca server publish back a message client just published


I'm making an IoT project that using mosca server, mqtt.js and express and jQuery for making a web. My purpose is making a website that can communicate with some embeded board such as Arduino, esp,...

I want the server publish back 'Hello World' when I publish some message to server. But the problem is when I do it, server sent back 'Hello World' and the message I just published.

Here is the example code:

server mosca

var mosca = require("mosca");

var settings = {
    http: {
        port: 8080,
        bundle: true,
        static: './'
    }
};

var server = new mosca.Server(settings);

server.on('clientConnected', function(client) {
    console.log('client ' +  client.id+ ' connect');
});

server.on('clientDisconnected', function(client) {
    console.log('client ' +  client.id+ ' disconnect');
});

// fired when a packet is send from server and is received from client
server.on('published', function(packet, client) {
    // console.log('Published', packet);
    if(typeof packet.payload == "object"){
            console.log(packet);
            console.log("From Client(Buffer) : "+ packet.payload.toString());
            var mesFromServer = {
                        topic:packet.topic,
                        payload:"Hello World",
                        qos:0,
                        retain:false
                    };
            server.publish(mesFromServer,client);
    }
});

server.on('ready', setup);

// fired when the mqtt server is ready
function setup() {
    console.log('Mosca server is up and running');
}

client.js using mqtt.js

$(document).ready(function(){
    $("#btnConnect").click(function(){

        $("#Connect-loader").css("display","inline");
        var client  = mqtt.connect({ host: 'localhost', port:8080 });
    
        client.on('connect', function () {
            if(client.connected)
            {
                $("#Connect-loader").css("display","none");
                $("#btnConnect").prop("disabled",true);
                $("#btnDisconnect").prop("disabled",false);
                $("#connect-notification").html("Server connected");
                $("#connect-notification").css("color","green");
            }
        });
        
        client.on('message', function (topic, message,packet) {
            // message is Buffer
            console.log(packet);
            $("#txtDataFromServer").val($("#txtDataFromServer").val()+"From Server: " + message.toString()+"\n");
        });

        client.on('close',function(){
            $("#btnConnect").prop("disabled",false);
            $("#btnDisconnect").prop("disabled",true);

            $("#connect-notification").html("Server disconnected");
            $("#connect-notification").css("color","red");
        });

        $("#btnSend").click(function(){
            var topic = $("#txtTopic").val();
            var mess = $("#txtMessage").val();
            
            if(topic != "" && mess !=""){
                client.subscribe(topic);
                client.publish(topic,mess,{qos:0,retain:false});
            }
        });
        
        $("#btnDisconnect").click(function(){
           client.end(); 
        });
    });
});

index.js using express

var express = require("express");
var app = express();

app.use(express.static("public")); 
app.use(express.static("node_modules"));

app.set("view engine","ejs");
app.set("views","./views");
var server = require("http").createServer(app);

server.listen(8000);

app.get("/",function(req,res){
    res.render("home");
});

I start the server mosca and index.js:

node server.js

node index.js

then I hit a button in my web to connect to mosca and then sent a message 'hello mqtt + mosca' to it but it sent back to me 2 messages.

From Server: hello mqtt + mosca

From Server: Hello World

And I don't expect the result 'hello mqtt + mosca'. How can I prevent the server publish back to me the message I publish to it ?


Solution

  • The short answer is you don't.

    If the client subscribes to a topic it will receive ALL messages published on that topic regardless of who published it.

    This is just how MQTT works.