node.jsmqttaedes

Cannot connect to Aedes MQTT broker on MacOs


I'm writing a simple MQTT Server using aedes and Nodejs.

Here is my broker code:

import aedes from "aedes";
import net from "net";

const port = 1883; // MQTT Port
const broker = net.createServer(aedes.handle);

broker.listen(port, () => {
  console.log(`Find the server at: mqtt://localhost:${port}/`); 
});

As the client I'm using MQTT X for MacOS Ventura 13.2:

enter image description here

I cannot connect to the server. The client times out without connecting. I've tried changing localhost to 127.0.0.1 with no success...

If I try telnet I can see the port is open:

$ telnet localhost 1883
Trying ::1...
Connected to localhost.
Escape character is '^]'.
sdfsdf
^C

What may be causing my client not to connect to the broker? How to make it connect?


Solution

  • The MQTT protocol handler is not being attached to the socket, so it's not processing the CONNECT packet from the client.

    Not sure why but if you rewrite it to not use the import statement and it works:

    const aedes = require('aedes')()
    const server = require('net').createServer(aedes.handle)
    const port = 1883
    
    server.listen(port, function () {
      console.log('server started and listening on port ', port)
    })