Mosquitto, the MQTT broker Part 1

With IOT market bursting out in ways we can’t even imagine the amount the data
that is going to be transferred is humongous. With the idea of connecting
each physical device to the internet the amount of traffic that is generated will increase many folds in the coming future. While all the pep talk is about big data, cloud and its
related technology there is not that much buzz about machine to machine communications.

Lets take a real world example and imagine that all our devices are connected to the
internet (World with IoT). Your Microwave oven might receive messages from the refrigerator asking it to preheat.  It can again relay traffic to television which is in close proximity. What if you want to turn on your room light from your office. What if you want your air conditioner to be turned on as soon as your daughter reaches home. What if you want your washing machine to wash all the clothes and keep it dry without you being nearby. There are a lot of what if’s here but i want to stop right it there.

With the above scenario the amount of data that is getting transferred is not huge but small, may be even matter of bytes. You need a reliable way to communicate information between the devices and with little overhead. This is where MQTT protocol is going to play a huge role. Read ahead to know more.

MQTT Protocol:

MQTT stands for Message Queuing Telemetry Transport. It works on top of TCP/IP to send/receive messages based on publisher-subscriber method. This requires a message broker for distributing messages to interested clients based on the topic of a message. The main intention of this blog is to showcase the capability and working of a message broker.  As this protocol is based on ISO/IEC standard we can rest be assured that this protocol is going to stay here for at least sometime.

MQTT methods:

Protocol defines a few methods which indicates the desired action that needs to be carried out. Referred from Wikipedia.

1. Connect : Waits for a connection to be established with the server.
2. Disconnect: Waits for the MQTT client to complete any work it must do.
3. Subscribe: Waits for completion of the Subscribe or Unsubscribe method.
4. Unsubscribe: Requests the server unsubscribe the client from one or more topics.
5. Publish: Returns immediately to the application thread after passing the request to the MQTT client.

I shall share more details about the methods with some sample packet captures.

MQTT brokers:

Mosquitto, is an Open Source MQTT broker which helps us to send/receive messages. This is most popular of all the available MQTT brokers in the linux world. As of this writing the version of mosquitto supported MQTT v3.1 version.

Host machine requirements:

Any linux machine can act as broker and any linux machine can act as client.
In my case i am using a ubuntu mate 16.04 machine as broker and ubuntu 12.04 LTS machine as client. And also I have tested this with different clients like banana pro, pandabaord ES, cubietruck, beaglebone black and RPI. I have used the above devices in different scenarios just to make sure things work as expected.


Execute the command in the both the linux machines to install mosquitto MQTT broker.

kasi@kasi-desktop:~$ sudo apt install mosquitto mosquitto-clients mosquitto-dev
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages were automatically installed and are no longer required:
linux-headers-4.4.0-31 linux-headers-4.4.0-31-generic linux-headers-4.4.0-38 linux-headers-4.4.0-38-generic linux-image-4.4.0-31-generic
linux-image-4.4.0-38-generic linux-image-extra-4.4.0-31-generic linux-image-extra-4.4.0-38-generic
Use ‘sudo apt autoremove’ to remove them.
The following NEW packages will be installed:
mosquitto mosquitto-clients mosquitto-dev
0 upgraded, 3 newly installed, 0 to remove and 49 not upgraded.
Need to get 146 kB of archives.
After this operation, 478 kB of additional disk space will be used.
Get:1 xenial/universe amd64 mosquitto amd64 1.4.8-1build1 [108 kB]
Get:2 xenial/universe amd64 mosquitto-clients amd64 1.4.8-1build1 [31.0 kB]
Get:3 xenial/universe amd64 mosquitto-dev all 1.4.8-1build1 [7,714 B]
Fetched 146 kB in 0s (1,100 kB/s)
Selecting previously unselected package mosquitto.
(Reading database … 325212 files and directories currently installed.)
Preparing to unpack …/mosquitto_1.4.8-1build1_amd64.deb …
Unpacking mosquitto (1.4.8-1build1) …
Selecting previously unselected package mosquitto-clients.
Preparing to unpack …/mosquitto-clients_1.4.8-1build1_amd64.deb …
Unpacking mosquitto-clients (1.4.8-1build1) …
Selecting previously unselected package mosquitto-dev.
Preparing to unpack …/mosquitto-dev_1.4.8-1build1_all.deb …
Unpacking mosquitto-dev (1.4.8-1build1) …
Processing triggers for man-db (2.7.5-1) …
Processing triggers for ureadahead (0.100.0-19) …
Processing triggers for systemd (229-4ubuntu10) …
Setting up mosquitto (1.4.8-1build1) …
Setting up mosquitto-clients (1.4.8-1build1) …
Setting up mosquitto-dev (1.4.8-1build1) …
Processing triggers for systemd (229-4ubuntu10) …
Processing triggers for ureadahead (0.100.0-19) …

As soon as the package is installed, mosquitto daemon is also started.

kasi@kasi-desktop:~$ service mosquitto status
● mosquitto.service – LSB: mosquitto MQTT v3.1 message broker
Loaded: loaded (/etc/init.d/mosquitto; bad; vendor preset: enabled)
Active: active (running) since Thu 2016-10-27 13:04:19 IST; 12s ago
Docs: man:systemd-sysv-generator(8)
CGroup: /system.slice/mosquitto.service
└─9861 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Oct 27 13:04:19 kasi-desktop systemd[1]: Starting LSB: mosquitto MQTT v3.1 message broker…
Oct 27 13:04:19 kasi-desktop mosquitto[9853]: * Starting network daemon: mosquitto
Oct 27 13:04:19 kasi-desktop mosquitto[9853]: …done.
Oct 27 13:04:19 kasi-desktop systemd[1]: Started LSB: mosquitto MQTT v3.1 message broker.

To check mosquitto version running on the host.
kasi@kasi-desktop:~$ mosquitto
1477553766: mosquitto version 1.4.8 (build date Fri, 19 Feb 2016 12:03:16 +0100) starting
1477553766: Using default config.
1477553766: Opening ipv4 listen socket on port 1883.
1477553766: Error: Address already in use

It listens on tcp socket and on port 1883 for both IPv4 and IPv6.

kasi@kasi-desktop:~$ sudo netstat -nlp | grep -i mosquitto
tcp 0 0* LISTEN 9861/mosquitto
tcp6 0 0 :::1883 :::* LISTEN 9861/mosquitto

With the installation done properly and mosquitto daemon listening on port 1883 we just need to execute the below command on the host machine (i have chose ubuntu mate 16.04 as my host) which is going to act as broker. With the below command MQTT broker starts to listen for the messages sent on a particular topic.
kasi@kasi-desktop:~$ sudo mosquitto_sub -h -t “mqtt” -v
[sudo] password for kasi:

The options are very less and more obvious/self explanatory.

-h points to the mqtt host to connect to, in this case the IP address of ubuntu mate 16.04 machine.
-t mentions the mqtt topic to subscribe to. Any arbitrary topic can be chose to listen for messages

From the client side (ubuntu 12.04 LTS machine), we can now start to send messages to the particular topic of interest using the below command:

kasi@intel:~$ mosquitto_pub -h -t “mqtt” -m “Hello from”

As soon as the above command is executed,  we get the below output
on the MQTT broker machine:

kasi@kasi-desktop:~$ sudo mosquitto_sub -h -t “mqtt” -v
[sudo] password for kasi:
mqtt Hello from

Things cannot be more simple than this when it comes to sending valuable information. The possibilities with this protocol is limited by only developers requirement as it can be customised to suit  various needs.

In Part 2 lets look at the network trace of what is happening in the background.