1
votes

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.

3
What is in the mosquitto logs when the NodeJS client tries to connect?hardillb
As I said..the werid thing is that the logs are empty , not sure if the log function was not able to run.joe
Stop mosquitto and run it manually with just -v on the cmd linehardillb
well if I use mosquitto -v to start another mosquitto process on port 1884, everything works fine.But not that mosquitto service,it still shows Not authorized on port 1883.joe

3 Answers

1
votes

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.

-1
votes

You need to add the authorize information to mqtt connect method.Just like this.

var client=mqtt.connect("ws://192.168.1.1", {
            username: "yourUsername",
            password: "yourPassword"
        }
-1
votes

Add the Authorization details for the client to connect

var mqtt = require('mqtt')
var client  = mqtt.connect('mqtt://192.168.1.70', {
                              username: '<username>', 
                              password: '<password>'
              });

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()
})