简体   繁体   中英

Mosquitto persitence problems with mqtt nodejs module

I am trying to run basic queue implementation using mosquitto broker, It works fine when the consumer and producer is running normally. If i run the producer and then i run the consumer i am unable to get the message(Data). I am using qos:2 while publishing the message on to the topic. I am using nodejs language with mqtt.js

consumer.js

const mqtt = require ('mqtt');
var options = {}
options.clientId = 'mqttjs_consumer'
options.clean = false
options.debug = true
var client  = mqtt.connect('mqtt://localhost',options);

client.on('connect', function () {
  client.subscribe('topic/sample',function(){
    console.log('client has subscribed successfully');
  });

});

client.on('message', function (topic, message){
  console.log(message.toString());
});

Producer.js

const mqtt = require ('mqtt');
var options = {}
options.clientId = 'mqttjs_producer'
options.debug = true
var client  = mqtt.connect('mqtt://localhost',options);

client.on('connect', function(){
  client.publish('topic/sample','message_data',{qos:2},function(err,status){
    console.log(err,status)
    process.exit(0)
  })
});

You need to subscribe at QOS 2 not just publish with QOS 2.

const mqtt = require ('mqtt');
var options = {}
options.clientId = 'mqttjs_consumer'
options.clean = false
options.debug = true
var client  = mqtt.connect('mqtt://localhost',options);

client.on('connect', function () {
  client.subscribe('topic/sample',{qos:2}, function(){
    console.log('client has subscribed successfully');
  });

});

client.on('message', function (topic, message){
  console.log(message.toString());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM