Can't connect to aedes MQTT broker

3.1k Views Asked by At

I've got an aedes MQTT broker and my MQTT client, but I can't seem to connect them.

In my app.js I do the following:

(async function () {
  try {
    await startBroker();
    await mqttClient();
  } catch (e) {
    console.error("ERROR: ", e);
    process.exit();
  }
})();

My startBroker function start aedes and streams it like so:

const aedes = require('aedes')();
const server = require('net').createServer(aedes.handle);
const port = 1883;

exports.startBroker = function() {
    return new Promise((res, rej) => {
        server.listen(port, function () {
            console.log(`MQTT Broker started on port ${port}`);
            return res()
        });
    })
};

and then my mqttClient tries to connect, however I can never get to actually connect. I've tested it against the test mosquitto server which works fine

const mqtt = require('mqtt');

const client = mqtt.connect("mqtt://localhost:1883");

exports.mqttClient = function() {
    console.log("Connecting to MQTT Client");
    client.on("connect", ack => {
        console.log("MQTT Client Connected!");

        client.on("message", (topic, message) => {
            console.log(`MQTT Client Message.  Topic: ${topic}.  Message: ${message.toString()}`);
        });
    });

    client.on("error", err => {
        console.log(err);
    });
}

Does anyone know why my broker doesn't seem to be working?

2

There are 2 best solutions below

0
On

Could you clarify, how the broker is not working and what actually works? Where and how do you run the code?

When I put the code into a single file (changing exports. into const), it does work. I had to add a semicolon after the function declaration of mqttClient, but after that, I get the following console output:

MQTT Broker started on port 1883
Connecting to MQTT Client
MQTT Client Connected!

This is the full code that can be copied right away. It runs in Node.js v12.15.0 on my macOS 10.15 Catalina.

const aedes = require('aedes')();
const server = require('net').createServer(aedes.handle);
const port = 1883;

// exports.startBroker = function() {
const startBroker = function() {
    return new Promise((res, rej) => {
        server.listen(port, function () {
            console.log(`MQTT Broker started on port ${port}`);
            return res()
        });
    })
};

const mqtt = require('mqtt');

const client = mqtt.connect("mqtt://localhost:1883");

//exports.mqttClient = function() {
const mqttClient = function() {
    console.log("Connecting to MQTT Client");
    client.on("connect", ack => {
        console.log("MQTT Client Connected!");

        client.on("message", (topic, message) => {
            console.log(`MQTT Client Message.  Topic: ${topic}.  Message: ${message.toString()}`);
        });
    });

    client.on("error", err => {
        console.log(err);
    });
}; // <-- semicolon added here

(async function () {
  try {
    await startBroker();
    await mqttClient();
  } catch (e) {
    console.error("ERROR: ", e);
    process.exit();
  }
})();
0
On

this is my code for setting up mqtt broker with aedes. For settings up wss (connection with SSL) you will have to added one more configuration just like the ws part. Let me know if you need code for SSL also.

const express = require('express');
const aedes = require('aedes')();
const ws = require('websocket-stream');
const fs = require("fs");
const server = require('net').createServer(aedes.handle);
const app = express();


const ports = {
    mqtt : 1883,
    ws : 8080,
    wss : 8081,
}


const host = '0.0.0.0' // localhost


server.listen(ports.mqtt, function () {
    console.log(`MQTT Broker running on port: ${ports.mqtt}`);
});


// -------- non-SSL websocket port -------------
var wsServer = require('http').createServer(app)
ws.createServer({ server: wsServer}, aedes.handle)
wsServer.listen(ports.ws, host, function () {
    console.log('WS server listening on port', ports.ws)
})