8
votes

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.

5
If you want to use your computer as MQTT broker, you should install mosquitto. It is MQTT daemon.Oleg Olivson
watta idiot! I've unistall it and I forget to re-install it! thanks so much!Luca Davanzo
Glad you solved your problem but please delete this question. It won't help anyone in the future.shellter
The source code of the utilities is with that of the broker in the package at mosquitto.orgjpmens
If I want to post in a server, do I need of mosquitto deamon running inside server?Luca Davanzo

5 Answers

13
votes

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
6
votes

For future googlers:

You can use a public host as mentioned above, but to start a local mosquitto broker, first make sure you have installed mosquitto in addition to mosquitto_sub. You can then start up the mosquitto broker by simply running the following:

mosquitto
3
votes

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)

2
votes

This is happening because you have installed only mosquitto clients on your system and not installed mosquitto on your system. please execute below command to install the MQTT Broker.

sudo apt-get install mosquitto

source: connection attempt failed bytesofgigabytes.com

1
votes

Be sure Your mosquitto service installed and correctly running.

for install : sudo apt-get install mosquitto

after install : sudo service mosquitto stop , sudo service mosquitto start