moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

Packets saved in persistence when client is online #700

Closed ITShibaInu closed 6 years ago

ITShibaInu commented 6 years ago

I am using mosca with mosquitto as backend and mongo as persistence. Here is my config: `var pubsubsettings = { type: 'mqtt', json: false, mqtt: require('mqtt'), host: '127.0.0.1', port: 1883 };

var moscaSettings = { port: 1884, //mosca (mqtt) port backend: pubsubsettings, //pubsubsettings is the object we created above persistence: { factory: mosca.persistence.Mongo, url: mongo.connectionString } };`

Somehow the persistence function doesn't work as expected. For example:

1. client A subscribes to topic/sth with clean:false 2. client B publishes to topic/sth with message 'hello' 3. client A receives message 'hello' -----everything is fine at the moment----- 4. client A goes offline 5. client B publishes to topic/sth with message 'hello' 6. client A goes online 7. client A receives message 'hello' TWICE <-expect to get one 'hello' only

I looked into the database and found that when client A is online and client B publishes to topic/sth (1. - 2.), the packet is saved to 'packet' collection in database. When client A goes offline and client B publishes one more 'hello' to topic/sth (4. - 5.), it also saves to 'packet' collection in database. At the moment we have two packets in database. That's why client A receives the message twice when it is online.

So is there any way to handle this? Or maybe code change?

mcollina commented 6 years ago

This is likely due to using the MQTT backend with MongoDB. Just use mongo (or better redis) everywhere.

If this is a new project, I recommend using https://github.com/mcollina/aedes.

ITShibaInu commented 6 years ago

thank for the advice! now im trying to use aedes and i got an issue there. i have opened a new ticket there about aedes-persistence-mongo.