mosca icon indicating copy to clipboard operation
mosca copied to clipboard

Cannot publish message from broker to client [help wanted]

Open BrainCrumbz opened this issue 11 years ago • 15 comments

Dear sirs

thanks for this library, we're using it in a demo and until now everything is working great. I don't know if your are monitoring StackOverflow for MQTT-related questions, nor I could find a mosca tag there, so here I'm linking the problem we have:

http://stackoverflow.com/questions/30035552/how-to-send-message-from-a-mosca-broker-to-a-mqtt-js-client

We don't even know if it's a matter of mosca or MQTT.js.
It could be great if someone could have a look at this.

Thanks for your time Cheers

BrainCrumbz avatar May 04 '15 16:05 BrainCrumbz

Can you post a full example? A similar code runs fine in the unit test. Even if it is not specified, the payload MUST be a Buffer in Mosca. Il giorno lun 4 mag 2015 alle 18:56 BrainCrumbz [email protected] ha scritto:

Dear sirs

thanks for this library, we're using it in a demo and until now everything is working great. I don't know if your are monitoring StackOverflow for MQTT-related questions, nor I could find a mosca tag there, so here I'm linking the problem we have:

http://stackoverflow.com/questions/30035552/how-to-send-message-from-a-mosca-broker-to-a-mqtt-js-client

We don't even know if it's a matter of mosca or MQTT.js.

It could be great if someone could have a look at this.

Thanks for your time Cheers

— Reply to this email directly or view it on GitHub https://github.com/mcollina/mosca/issues/249.

mcollina avatar May 05 '15 05:05 mcollina

Thanks for quick reply. We'll put up a sample project, see if the issue persists and post that here.

On a side note, can you please tell us what's the unit test running this scenario, so that we can have a look at its setup?

Cheers

BrainCrumbz avatar May 05 '15 07:05 BrainCrumbz

https://github.com/mcollina/mosca/blob/master/test/abstract_server.js#L2055-L2081

Il giorno mar 5 mag 2015 alle ore 09:28 BrainCrumbz < [email protected]> ha scritto:

Thanks for quick reply. We'll put a sample project, see if the issue persists and post that here.

On a side note, can you please tell us what's the unit test running this scenario, so that we can have a look at its setup?

Cheers

— Reply to this email directly or view it on GitHub https://github.com/mcollina/mosca/issues/249#issuecomment-98982295.

mcollina avatar May 05 '15 07:05 mcollina

Just to be sure: we're talking about a scenario similar to the one found in Basic Usage Wiki, section Sending data from mosca to clients.

Is this test for that scenario? I could not find any server.publish call there.

BrainCrumbz avatar May 05 '15 08:05 BrainCrumbz

in the code instance is a Mosca instance. https://github.com/mcollina/mosca/blob/master/test/abstract_server.js#L2092-L2118.

mcollina avatar May 05 '15 08:05 mcollina

Thanks. So, here's a gist with client- and server-related files (instructions are just for future reference, not really needed for you I think :) ).

This demo now works, at least the client can see the message published by server. For some reason, don't know why, the server seems to run the onCreated callback twice, while the 'ready' event is correctly fired just once.

No idea why this happens, and if it's a problem. Nor why the message does not work in our actual project.

BrainCrumbz avatar May 05 '15 09:05 BrainCrumbz

For ease of use, there's a simple Github project as well.

BrainCrumbz avatar May 05 '15 09:05 BrainCrumbz

Update we went on with more tests/demos, and we think we found an issue (at least in our code):

when we switch from in-memory persistence to mongoDb-based persistence, the messages stop being forwarded/sent from broker to subscribers. This happens both in our actual project, as well as in sample Github project.

So at least this means there's some problem in the way we setup mondoDb persistence. Here are our pub-sub settings when relying persistence on mongoDb. Actually, it's a development instance running on mongolab.com:

var mongoDbSettings = {
  // remote MongoLab mongodb instance
  url: 'mongodb://mqtt-db-user:<OUR-USER-CODE>@<THEIR-INSTANCE-SUBDOMAIN>.mongolab.com:39291/sample-mqtt-datastore',
  pubsubCollection: 'myCollections',
};

var mongoDbBrokerSettings = {
  port: 1883,  // mosca (mqtt) port

  // using ascoltatore over MongoDB
  backend: {
    type: 'mongo',
    url: mongoDbSettings.url,
    pubsubCollection: mongoDbSettings.pubsubCollection,
    mongo: {},
  },
  persistence: {
    factory: mosca.persistence.Mongo,
    url: mongoDbSettings.url,
  }
};

var broker = new mosca.Server(mongoDbBrokerSettings, function onCreated(err, broker) {
  // assume no errors

  console.log('MQTT broker is up and running');
});

does anyone spot anything wrong with that?

Thanks all

BrainCrumbz avatar May 05 '15 17:05 BrainCrumbz

Not really, all seems good.

mcollina avatar May 11 '15 15:05 mcollina

Uhm. Then I don't know. Memory persistence is ok for now, as long as we're in demo/prototype phase. But sooner or later we'll have to move to a proper pub-sub persistence like MongoDb.

Are there other example sources on how to setup MongoDb pub-sub? (BTW, thanks for your attention)

BrainCrumbz avatar May 11 '15 19:05 BrainCrumbz

Here is the configuration we run in the tests: https://github.com/mcollina/mosca/blob/master/test/server_mongo.js#L33-L51

mcollina avatar May 18 '15 06:05 mcollina

Thanks a lot, as soon as we go back to the mongodb settings, we'll try it. Regards

BrainCrumbz avatar May 18 '15 08:05 BrainCrumbz

i have the same problems with remote mongodb server, i fix it very easy...

  • create server without persistence
var server = new Mosca.Server({
  port : CONFIG.taxi_mqtt.port,
  stats : false
});
  • the server does not have persistence for the first messages, now define persistence engine
var Mongo = require('mongodb');
function connectPersist() {
  Mongo.MongoClient.connect(CONFIG.taxi_mqtt.persist, function(err, db) {
    if(err) {
      console.log("fail connect to: " + CONFIG.taxi_mqtt.persist);
      setTimeout(connectPersist, 2000);
    } else {
      console.log("Connected correctly to mongodb persistence");
      var persist = new Mosca.persistence.Mongo({
        connection : db
      });
     // here is the magic....
      persist.wire(server);
    }
  });
}
connectPersist();

so easy, mosca has a big problem with mongodb persist engine in connect time. some time fails, some time works, i try to fix it, but i dont have time. the problem is mosca do not fails, crash or launch event if mongo is unavailable or connect fails.

https://github.com/mcollina/mosca/blob/master/lib/persistence/mongo.js#L80-L86

mazhack avatar Jun 09 '15 21:06 mazhack

This is weird, I expected https://github.com/mcollina/mosca/blob/master/lib/persistence/mongo.js#L85 to crash Mosca.

mcollina avatar Jun 11 '15 07:06 mcollina

I upgraded Mosca to the top-most version of MongoDB driver last month. Hopefully the connection problems with MongoDB should be fixed, but let me know if they still persists.

mcollina avatar Jul 18 '15 14:07 mcollina