moscajs / mosca

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

Unsubscribe doesn`t work with MongoDB #533

Open xthuan opened 8 years ago

xthuan commented 8 years ago

@mcollina same problem as issue #382

Server: MongoDB(persistence) + Mosca running on linux Server (Sample example:mtqq_engine.js, build one broker with mosca)

Simulate_client: MQTT (Sample example:client_mosca_bug.js, set clean to false to receive the offline message, this client subscribes topic (/device001) at the beginning and unsubscribes the same topic after 20s.)

After client unsubscribed the topic(/device001), mongoDB should DELETE this topic of this client from Collection Subscriptions, AND it does NOT, the consequence is whenever restart this client without subscribing any topic, it will still receive the message from the Unsubscribed topic(/device001) which should NOT happen at all.

I checked #391, {+ that.server.persistClient(that);} is in my /lib/client.js, seems like the problem is not cured by it.

Currently I have to tell the mosca sever to access MongoDB to delete the corresponding topic whenever it received the UNsubscribe message.

Cheers, Xintao

server screenshot: server

client_generate_bug screenshot: client

MongoDB screenshot(the topic should be deleted): mongodb

server code: ` var mosca = require('mosca');

//use mongoDB as offline persistence backend var settings = { port: 5113, backend:{ type: 'mongo', url: 'mongodb://xxxxxxxxxx0.0.0.0:27017/mqtt', pubsubCollection: 'ascoltatori', mongo: {} }, persistence:{ factory: mosca.persistence.Mongo, url: "mongodb://xxxxxxxxxx0.0.0.0:27017/mqtt", } };

//create mqtt server and provice interface to other components var mqtt_engine = new mosca.Server(settings);

function setup(){ console.log("MQTT_BROKER IS RUNNING @PORT 5113"); }

mqtt_engine.on('ready', setup);

mqtt_engine.on('published', function(packet, client) { console.log('published', packet.payload.toString()); });

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

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

mqtt_engine.on('subscribed', function(topic,client) { console.log(client.id,' subscribed ',topic); });

mqtt_engine.on('unsubscribed', function(topic,client) { console.log(client.id,' unsubscribed ',topic); }); `

client_generate_bug code: ` var mqtt = require('mqtt');

var settings = { keepalive: 10, protocolId: 'MQIsdp', protocolVersion: 3, clientId: 'android', clean: false }

var client = mqtt.connect('mqtt://xxxxxxxxxxxx:5113',settings);

client.subscribe('/device001',{qos:1},function(){ console.log('#android subscribed #topic /device001 #ok.'); });

var num = 0; setInterval(function (){ client.unsubscribe('/device001',function(){ console.log('#android UNsubscribed #topic /device001 #ok.'); }); }, 20000); `

mcollina commented 8 years ago

I think there is a bug in https://github.com/mcollina/mosca/blob/master/lib/persistence/mongo.js#L180-L206

It's not removing the subscriptions when persistClient is called. Would you like to send a PR?