moscajs / mosca

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

Handling of buffered messages upon reconnect #772

Open kelsayed opened 5 years ago

kelsayed commented 5 years ago

I have a very simple application goes like: var server = new mosca.Server({ host: '127.0.0.1', port: 3333 });

server.on('clientConnected', function(client) { console.log('client connected', client.id); });

server.on('clientDisconnected', function(client) { console.log('client disconnected', client.id); });

server.on('published', function(packet, client) { mqttPublishHandle(packet.topic, client.id, packet.payload); });

The problem is, if I kill the server (broker) while publishers are running and bring the server up again, the publishers reconnect and send all queued messages. For some reason the publish event handler seems not to continue to completion for each of the received publish messages and some kind or race condition occurs. I was able to solve this in my test clients which are based on node mqtt by setting the queueQoSZero option to false as shown below. However, not all clients will be like that. Is it possible to have the server force the clients to drop/flush all queued messages upon reconnect. Is it possible to do it at the server side (but how can it tell if the messages are old or new?). Is it possible to continue the mqttPublishHandle function without interruption on the node.js execution engine? I thought this is how node.js works but this is not what I am observing in this case.

//code on the publisher var options = { port: 3333, host: '127.0.0.1', protocol: 'mqtt', queueQoSZero: false } var client = mqtt.connect(options);