Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mosquitto-client obtain refused connection

I want to use MQTT protocol using mosquitto library.

First of all, I want to do some test installing mosquitto-clients

 sudo apt-get install mosquitto-clients

This program provides two "method":

  • mosquitto_pub
  • mosquitto_sub

Following this instructions I'm trying to submit new topic:

mosquitto_sub -d -t newtopic/test

using default host/port [localhost/1883].

I obtain:

Error: Connection refused

Is too generic as error.. can anyone help me?
Could be is a firewall problem? In this case, how can I check if is this the problem?

I'm using linux ubuntu ( 3.8.0-42-generic #62~precise1-Ubuntu)

nb same behaviour writing custom program using libmosquitto.

like image 443
Luca Davanzo Avatar asked Jul 03 '14 14:07

Luca Davanzo


3 Answers

Just edit Mosquitto configuration file ( /etc/mosquitto/conf.d/mosquitto.conf ) adding these lines...
allow_anonymous true
listener 1883 0.0.0.0

... and restart Mosquitto (as service or not).
$ sudo service mosquitto restart
or
$ mosquitto --verbose --config-file /etc/mosquitto/conf.d/mosquitto.conf

As informed here, since v.1.7 allow_anonymous defaulted to false. It is also useful to check log messages ( /var/log/mosquitto/mosquitto.log ).

Finally, run Mosquitto subscriber/publisher using --host (-h) parameter and the host IP address (get if from ifconfig or ip -color addr command).

like image 101
T77 Avatar answered Oct 26 '22 06:10

T77


The default host:port combination for mosquitto_pub/sub is localhost:1883. If you do not have a broker running on your local computer then it will not be able to connect of course.

The solution is to either run the broker on your local computer, or to tell the utilities where to connect. For example:

mosquitto_sub -t newtopic/test -h test.mosquitto.org
like image 15
ralight Avatar answered Oct 26 '22 04:10

ralight


None of the other answers worked for me. In my case, I had upgraded from mosquitto 1.X to mosquitto 2.0, which requires a new configuration to be added to your mosquitto.conf:

listener 1883

For clients other than localhost to connect (ie, via Docker)

like image 13
user2085368 Avatar answered Oct 26 '22 04:10

user2085368