0

In my project I am trying to have multiple Raspberry Pi devices as "publishers" with one broker in the middle and countless subscribers to get info from certain topics.

I am having problems with connecting broker and publisher, it just stops on console.log("Before connection") and is unwilling to go further.

Broker:

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

server.listen(port, function() {
    console.log('Server je pokrenut na portu ', port)
})

Publisher:

const mqtt = require('mqtt');
const client = mqtt.connect('mqtt://127.0.0.1:1883')
console.log("Before connection")
client.on("connect", function () {
    console.log("Connected before interval")
    setInterval(function () {
        console.log("Before publish")
        client.publish('NameOfTopic', 'Hello mqtt');
        console.log('Message Sent');
        
    }, 5000);
});

I know something is happening because when I kill publisher, broker gets also killed with error event on Socket instance, error number -4077, code econnreset

cortex
  • 81
  • 5
  • That publisher code assumes it is being run the same machine as the broker (`127.0.0.1` is always the machine the code is running on) and you have nothing to check for any errors. – hardillb Mar 20 '21 at 15:46
  • Both broker and publisher are on same machine currently. How to check for errors? – cortex Mar 20 '21 at 15:47
  • 1
    The mqtt.js docs lists the other events you can register handlers for apart from `'connect'` e.g. `'error'` – hardillb Mar 20 '21 at 15:49

0 Answers0