Mosquitto is a BSD licensed message broker that implements the MQ Telemetry Transport protocol versions 3.1 and 3.1.1.
Mosquitto is a lightweight broker and it is available in binary form for most used platforms like Windows, Linux and Mac OS X.
Since it has been written in C language, and the code is small enough, you can compile it for your own platform. And more, installing from source could give you some boosts in performance since you can apply your own optimizations directives….
Let’s start installing mosquitto on a brand new raspbian SD Card.
- Power on your Raspberry Pi and login in a console.
- Install the development environment (if your raspbian does not have installed it)
$ sudo apt-get update
$ sudo apt-get install build-essential python quilt devscripts python-setuptools python3
- Install needed libraries
$ sudo apt-get install libssl-dev libwrap0-dev libc-ares-dev
Note, if you don’t install the libc-ares-dev library you will have the following error:
ares.h: No such file or directory In file included from mosquitto.c
- Download latest version of Mosquitto (take a look at http://mosquitto.org/download/) for version 1.3.4:
$ wget http://mosquitto.org/files/source/mosquitto-1.3.4.tar.gz
- extract the tarballed archive
$ tar zxvf mosquitto-1.3.4.tar.gz
- go to mosquitto directory
- Build from the sources
$ make all
- Install mosquitto into your system
$ sudo make install
- configure dynamic linker run-time bindings
$ sudo ldconfig
- Ok, your mosquitto broker is ready to run
mosquitto -d -c /etc/mosquitto/mosquitto.conf
Take a look to the /etc/mosquitto/mosquitto.conf configuration file to proper configure your broker.
Now you can try mosquitto:
- Open two terminal windows.
- In the first terminal run
$ mosquitto_sub -h 127.0.0.1 -t myTopic
- In the second terminal run
$ mosquitto_sub -h 127.0.0.1 -t myTopic -m "My first message"
$ mosquitto_pub -h 127.0.0.1 -t myTopic -m "My first message"
At this point, on the first terminal you should have received the message “My first message”