moscajs / mosca

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

The sequence of offline messages is not correct #682

Open penglinjiang opened 7 years ago

penglinjiang commented 7 years ago

Hi when I am trying to test the sequence of the offline messages, I found that the received offline messages are not in the order they were sent. Is there any method can solve this? The code is as follows:

mosca_server.js

var mosca = require('mosca');
var settings = {
    port: 1883,
    backend: {
        type: 'mongo',
        url: 'mongodb://127.0.0.1:27017/mqtt',
        pubsubCollection: 'ascoltatori',
        mongo: {}
    },
    persistence: {
        factory: mosca.persistence.Mongo,
        url: 'mongodb://127.0.0.1:27017/mqtt',
        ttl: {
            packets: 1000 * 60 * 60 * 24
        }
    }
};
var server = new mosca.Server(settings);

server.on('ready', function() {
    console.log('Mosca server is up ang running');
});

server.on('published', function(packet, client) {
    console.log("published", packet.payload.toString());
});
server.on('clientConnected', function(client) {
    console.log('client connected', client.id);
});
server.on('error', function(){
    console.log('mosca error');
})

pub_client.js

var mqtt = require('mqtt');

var client = mqtt.connect('mqtt://127.0.0.1:1883');

var num = 0;
setInterval(function(){
    client.publish('order', 'Hello mqtt' + (num++), {qos:1, retain: true});
    console.log('pub_client send a message', num);
}, 1000);

client.on('connect', function () {
    client.subscribe('order');
});

sub_client.js

var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://127.0.0.1:1883',{clientId:'1', clean:false});

client.subscribe('order', {qos:1});

client.on('message', function(topic, message){
    //message is buffer
    console.log(message.toString());
})
mcollina commented 7 years ago

I would recommend to use https://github.com/mcollina/aedes.

penglinjiang commented 7 years ago

We have mosca applied with the production environment,can mosca solve this problem? @mcollina

mcollina commented 7 years ago

I have no bandwidth to address this atm. I will accept a PR if you would like to send it.

psummo commented 6 years ago

@mcollina in my case, message arrive in wrong order and will publish always all message in that topic, not only the sent during offline