Reputation: 27
How get all messages when new client subscribe instead last one message (mqtt, retain)? I have two applications. I set retain, clean flag and qos: 1. I used same code for both applications. Example:
now test_user should be get all messages from user(app_1) [hello and hello2], but I get onlny "hello2"
let client = mqtt.connect('http://127.0.0.1:9001', {
clientId: "front-client",
username: "u",
password: "p",
clean: false
});
client.on('connect', (topic) => {
client.subscribe(this.topic, {qos: 1}, (err) => {
if (!err) {
console.log('message sent by mqtt')
}
});
});
this.mqttClient = client;
//handle incoming messages
client.on('message', (topic, message, packet) => {
//
});
sendMsgByMqtt(message: string) {
this.mqttClient.publish(this.topic, message, {retain: true, qos: 1});
}
Upvotes: 1
Views: 3372
Reputation: 59608
You don't
MQTT is not a Message Queuing system, it is a Pub/Sub system. MQTT will only queue messages for existing clients that are currently offline. There is NO way for a new client to see previous messages published to a topic.
The only exception is if the retained bit is set when the message is published and the broker will only retain the last message published to that topic with the retained bit set. Any new message with that flag set will replace the previous message.
Upvotes: 3