Reputation: 11
My MQTT.js client is unable to receive all messages from Mosquitto broker, due to constant disconnect/reconnects. When adding the option clean: false
to publish and subscribe, the client stops receiving messages altogether. I've tried all settings of qos
, with no success. I think I'm using the cleanSession improperly, does anybody know?
My configuration is as follows:
var options = {
clientId: "python_pub",
clean: false,
qos: 2
};
// connect to the message server
var client = mqtt.connect('mqtt://PATH_TO_BROKER', options);
client.on('connect', function() {
client.subscribe('topic_name');
})
client.on('message', function(topic, message) {
console.log("received: %s", message);
}
var count = 0;
while (count < 100) {
client.publish('test_topic', count.toString(), {qos: 2});
count++;
}
Upvotes: 1
Views: 1006
Reputation: 59608
QOS is not just a setting on the published message, it is also part of the subscription.
The QOS only covers one leg of the transmission of a message at a time. e.g. only between the publisher and the broker, not all the way to the client. If you want assured delivery to the client you need to subscribe at a QOS greater than 0 as well.
The qos
option in your options
object will be ignored.
Also you should look at why your client keeps disconnecting first. I see you have a hard coded clientid, this is normally not a good idea as clientids need to be globally unique across all clients connected to a broker. If a second client tries to connect to a broker with a clientid of a already connected client it will kick the first client off. If you have automatic reconnection enabled then this will just lead to both clients fighting and kicking each other off the broker which sounds like the problem you described.
Upvotes: 2