javascript - mosquitto+mqtt.js got "Connection refused: Not authorized" - Stack Overflow

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

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:

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:

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.

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:

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:

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.

Share Improve this question edited Aug 21, 2017 at 8:29 joe asked Aug 18, 2017 at 2:06 joejoe 1,3182 gold badges13 silver badges34 bronze badges 4
  • What is in the mosquitto logs when the NodeJS client tries to connect? – hardillb Commented Aug 18, 2017 at 6:45
  • As I said..the werid thing is that the logs are empty , not sure if the log function was not able to run. – joe Commented Aug 18, 2017 at 9:14
  • Stop mosquitto and run it manually with just -v on the cmd line – hardillb Commented Aug 18, 2017 at 13:38
  • 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 Commented Aug 21, 2017 at 1:52
Add a ment  | 

3 Answers 3

Reset to default 2

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.

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"
        }

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745161074a4614392.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信