MQTT.js icon indicating copy to clipboard operation
MQTT.js copied to clipboard

can't not publish message after reconnect when I using mqtt-level-store with qos > 0

Open byterygon opened this issue 2 years ago • 2 comments

Hey I'm new to mqttjs I'm trying to use level store to persistence message while disconnect to the broker here is my example code:

const mqtt = require("mqtt");
const levelStore = require("mqtt-level-store");
const manager = levelStore("./mqttMessage/");
const client = mqtt.connect("mqtt://127.0.0.1", {
  incomingStore: manager.incoming,
  outgoingStore: manager.outgoing,
});
client.on("connect", function () {
  client.subscribe("presence", function (err) {
    if (!err) {
    }
  });
});

client.on("message", function (topic, message) {
  debug(message.toString());
});

const debug2 = require("./src/utils/debug")("publish");
let i = 0;
setInterval(() => {
  debug2("sending message " + i);
  client.publish("presence", `${i}`, { qos: 2 }, (err) => {
    debug2(err || "sent " + i++);
  });
}, 3000);

here is the log: image I stop the broker after receive the 3rd (it's 4th but I will call it's 3rd to match the i value) message, after the interval triggered twice, I start the broker again the 4th message that has been store is send completely, The 5th message has been sent, but the subscriber could not receive any thing, you can see that the publish callback has been triggered and no err. The 6th message, no message sent and the publish call back has not been triggered as you can see

Sorry for my English.

I'm using "mqtt": "^4.3.6", "mqtt-level-store": "^3.1.0", "node": "^16.14.0"

byterygon avatar Mar 22 '22 07:03 byterygon

Hello, I also found this problem in the development of WeChat Mini Program. When I switch the program to the background and then switch back, it also hangs and does not receive the callback of publish. I expect to disconnect + reconnect (connect) to fix it, but still doesn't work. Have you solved this problem now, and if so, what kind of solution did you take? Thank you very much

wanggaian avatar Sep 11 '22 03:09 wanggaian

@wanggaian I have solved this problem by changing to nedb. I hope this may help you too.

byterygon avatar Sep 12 '22 03:09 byterygon