How to send message from mosca broker to mqtt.js client

we are setting up an MQTT client using MQTT.js (either on a desktop pc or an actual board, same thing) and the MQTT Broker runs on mosca which runs on the desktop pc.

We can successfully send a message with a specific topic from client to broker, but now we are stuck in the opposite: send a message from broker to client .

We follow the code samples provided in both project docs but no luck. The client can register correctly for the topic, but does not respond in any way to sending the message. The broker can send the message correctly as well as listen to his own message, since he is listening for all messages anyway.

Excerpt from MQTT Broker:

broker.on('published', function(packet, client) {
  console.log('MQTT broker received message');  // it passes by here
  console.log(' - packet:', packet);
  console.log(' - packet payload:', packet.payload.toString());
});

// ...

var packet = {
  topic: 'our_custom_topic',
  payload: ourCustomPayload,
  qos: 1,
  retain: false,  
};

console.log('MQTT broker sending message to client ...');

broker.publish(packet, function() {
  console.log('Message sent');  // it passes by here
});

      

Excerpt from MQTT client:

client.on('connect', function () {
  console.log('MQTT client connected');

  client.subscribe('our_custom_topic', { qos: 1 }, function onSubscribe(err, granted) {
    if (err) {
      console.log('subscribe errors:', err);  // no error shown
    }

    if (granted) {
      console.log('subscribe granted:', granted);  // it passes by here
    }
  });
});

// ...

client.on('message', function (topic, message, packet) {
  console.log('MQTT client received message');  // it DOESN'T pass by here
  console.log(' - topic', topic);
  console.log(' - message', JSON.stringify(message));
  console.log(' - packet', JSON.stringify(packet));
});

      

Update

The problem seems to come up when persistence is based on mongoDb. Instead, messages are correctly sent to subscribers when the save is memory-based. Here are our pub-sub settings when you rely on persisting to mongoDb. In fact, this is a development instance running at 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');
});

      

can anyone detect something wrong with this?

+3


source to share


2 answers


MQTT broker processes messages between clients, does not send them (except for special Last Will Testament messages).

You need to implement the following:

  • Client A subscribes to topic: test / one
  • Client B posts a post on the same topic: test / one
  • Automatically, all subscribed clients in this section (for example, client A) will receive the message.


But yes in Mosca you can instantiate a client object that acts as an MQTT client and can publish.

You have to debug that these posts are published efficiently with an external client like mosquitto_sub or MQTT.fx subscribed to our_custom_topic

.

Persistence should not affect post posting, but you can install mosquitto and test if used as backend works https://github.com/mcollina/mosca/wiki/Mosca-advanced-usage p>

+1


source


You can publish directly to the broker as it is part of the public Mosca API:

/**
 * Publishes a packet on the MQTT broker.
 *
 * @api public
 * @param {Object} packet The MQTT packet, it should include the
 *                        topic, payload, qos, and retain keys.
 * @param {Object} client The client object (internal)
 * @param {Function} callback The callback
 */
Server.prototype.publish = function publish(packet, client, callback)...

      



As long as your client is signed up, you can send via broker.publish({topic:"/foo/bar", payload:'foo'}, client)

on the client (i.e. from the callback) the model instance you want to send.

0


source







All Articles