Saturday 3 June 2017

how can I send the disconnect messages to subscriibed clients if one client is disconnected in MQTT?

Hello All I tried client 1 and client 2 program I can able to easily communicate with them.I can easily send the messages and receive the messages with them.but I don't know if one client is disconnected, how can I send the disconnected message to subscribed clients.can anyone help me.I need urgently.Thanks in advance.I am waiting for your reply.

client 1:


var mqtt=require("mqtt");
var express=require("express");
var app=express();
var options={
    keepalive:100,
    port: 1883,
    clientId:'1',
    clientSession:false,
    host: "http://localhost:8000",
    will:
        {
            topic:'willMag',
            payload:"connection closed abnormallly r",
            qos:1,
            retain:true
        }
};
var client=mqtt.connect("tcp://192.168.43.137:1883",options);
client.on("connect",function()
{
    setInterval(function()
    {
        client.publish("ranjith/princy","hello love you princy",function()
        {
            console.log("message published in client1");
        });
    },2000);
    client.subscribe("bv/nivi");
    client.on("message",function(topic,message)
    {
            console.log("I recieved the topic:"+topic);
            console.log("I recieved the message:"+message);
    });
});
client.on("disconnect",function()
{
    console.log("disconnected client1");
});
app.listen(8000,function()
{
    console.log("server listen at port 8000");
});



client 2:
var mqtt=require("mqtt");
var express=require("express");
var app=express();
var options={
    keepalive:100,
    port: 1883,
    clientId:'2',
    clientSession:false,
    host: "http://localhost:8086",
    will:
        {
            topic:'willMag',
            payload:"connection closed abnormallly b",
            qos:1,
            retain:true
        }
};
var client=mqtt.connect("tcp://192.168.43.137:1883",options);
client.on("connect",function()
{
    setInterval(function(){
        client.publish("bv/nivi","hello love you nivi",function()
        {
            console.log("message published in client2");
        });
    },2000);

    client.subscribe("ranjith/princy");

        client.on("message",function(topic,message)
        {
            console.log("I recieved the topic:"+topic);
            console.log("I recieved the message:"+message);
        });

});
client.on("disconnect",function()
{
    console.log("disconnected client2");
});
app.listen(8086,function()
{
    console.log("server listen at port 8000");
});


via vigneshRavi

No comments:

Post a Comment