jlang jlang - 15 days ago 5
Node.js Question

Simple communication via MQTT between node apps

Hi I'm really new to MQTT and I've read a lot of posts and blogs about it the last days, yet I seem not to fully understand the different parts needed, such as Broker, Clients.

I want two node apps to communicate with each other via a local mqtt service. As far as I understand, this mqtt service is called broker. I managed it to let 2 node apps communicate via a public broker like this:

app1 (sender)

const mqtt = require('mqtt');

// to be changed to own local server/service
const client = mqtt.connect('http://broker.hivemq.com');

client.on('connect', () => {
let id = 0;
setInterval(() => {
client.publish('myTopic', 'my message - ' + ++id);
}, 3000);
});


app2 (receiver)

const mqtt = require('mqtt');

// to be changed to own local server/service
const client = mqtt.connect('http://broker.hivemq.com');

client.on('connect', () => {
client.subscribe('myTopic');
});

client.on('message', (topic, message) => {
console.log('MSG: %s: %s', topic, message);
});


As this worked, I wanted to move on by replacing the public broker with a private one. After a while I found mqtt-server as a node package.

So I tried the following as a third node app, which is supposed to be the broker for app1 and app2:

server (MQTT broker)

fs = require('fs');
mqttServer = require('mqtt-server');
let subscriptions = [];

let servers = mqttServer(
// servers to start
{
mqtt: 'tcp://127.0.0.1:1883',
mqttws: 'ws://127.0.0.1:1884',
},
// options
{
emitEvents: true
},
// client handler
function (client) {
client.connack({
returnCode: 0
});

client.on('publish', (msg) => {
let topic = msg.topic;
let content = msg.payload.toString();
// this works, it outputs the topic and the message.
// problem is: app2 does not receive them.
// do we have to forward them somehow here?
console.log(topic, content);

});

client.on('subscribe', (sub) => {
let newSubscription = sub.subscriptions[0];
subscriptions.push(newSubscription);
console.log('New subscriber to topic:', newSubscription.topic);
});
});

servers.listen(function () {
console.log('MQTT servers now listening.');
});


Problem



After adjusting the connection-Uris (both to
ws://127.0.0.1:1884
) in app1 and app2 The server app receives all messages that are published and recognises that someone connected and listens to a specific topic.

But: While the server gets all those events/messages, app2 does not receive those messages anymore. Deducing that, something with this broker must be wrong, since using the public broker everything works just fine.

Any help is appreciated! Thanks in advance.

Answer

I can't get mqtt-server to work either, so try Mosca.

Mosca only needs a back end if you want to send QOS1/2 messages, it will work with out one.

var mosca = require('mosca');

var settings = {
    port:1883
}

var server = new mosca.Server(settings);

server.on('ready', function(){
  console.log("ready");
});

That will start a mqtt broker on port 1883

You need to make sure your clients are connecting with raw mqtt not websockets, so makes sure the urls start mqtt://