Search code examples
javascriptnode.jsmqttmosquitto

mosquitto+mqtt.js got "Connection refused: Not authorized"


I built mosquitto on CentOS7 and a node.js client based on mqtt.js,installing with

yum install mosquitto mosquitto-clients

The local test

> mosquitto_sub -h localhost -t test

> mosquitto_pub -h localhost -t test -m "hello world"

works fine, but when I ran:

var mqtt = require('mqtt')
var client  = mqtt.connect('mqtt://192.168.1.70')

client.on('connect', function () {
  client.subscribe('presence')
  client.publish('presence', 'Hello mqtt')
})

client.on('message', function (topic, message) {
  // message is Buffer
  console.log(message.toString())
  client.end()
})

I got Error: Connection refused: Not authorized

The mosquitto.conf is like:

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log
allow_anonymous true

and I use systemctl restart mosquitto to restart it several time, which doesn't help. The firewall is down and log file stays empty. A screenshot on status: enter image description here

Can anyone help please?

UPDATE:

It turns out that the mosquitto service is somehow broken as the status shows Active: active (exited). I use mosquitto -p 1884 -v cmd to run another mosquitto process on port 1884, it works fine. Then I try to reload the conf using > /etc/init.d/mosquitto reload. It gives me

Reloading mosquitto configuration (via systemctl): Job for mosquitto.service invalid. [FAILED]

So there IS something wrong with mosquitto service. Not a final solution but I manage to fix this by remove-reboot-install process, the status went green as follow:

Correct mosquitto status

SOLUTION

I managed to find out the reason it doesn't work. I've installed rabbitmq on my server, it uses its "rabbitmq_mqtt" which consumes port 1883. Reassigning a port will solve this problem.


Solution

  • I managed to find out the reason. I've installed rabbitmq on my server, it uses its "rabbitmq_mqtt" which consumes port 1883. Reassigning a port will solve this problem. The problem is simple, but yeah, the CLI should have given me more information.