Introduction to MQTT and configuration of a Mosquitto Broker

André Benevides
8 min readJul 3, 2021

INTRODUCTION

Hello, my name is André Benevides, I am a Software Engineer from Portugal and this will be the first article that I ever wrote. I have recently discovered MQTT while I was studying Event-Driven Architecture and since then I’ve been fascinated by its capabilities. I hope you enjoy the reading and that I can help you learn a bit.

WHAT IS MQTT?

MQTT stands for “Message Queuing Telemetry Transport” and is an OASIS messaging protocol that makes use of the publish/subscribe communication pattern.

The protocol was firstly designed for IoT devices but because of how powerful it is, it can also be used by full-fledged enterprise application server hosted in a cloud service.

HOW DOES IT WORK?

Example of the MQTT protocol

For those aware of what a publish/subscriber communication pattern is the image above should look very similar to that but for those that do not know the pattern yet I’ll attempt to explain it in an extremely simplified way:

This pattern defines how two or more entities communicate with each other, there are those that subscribers and those that are publishers, for example when you subscribe to a newsletter “Company A” and at the end of the month you receive an e-mail from “Company A”, you are the subscriber and “Company A” is the publisher.

In MQTT this pattern changes up a bit as there is not a direct connection between devices and instead the devices connect to an MQTT broker and tell it what topics they want to subscribe to and publish their messages to it. This is a great addiction because even if the connection between a device and a broker breaks for a while, the broker will buffer the messages received in the meantime and deliver them when the device reconnects. In a standard pub/sub implementation unless the publisher knows about all of its subscribers and maintains a buffer for each one, any messages sent while one of the devices is disconnected would be lost.

WHY IS IT USED IN IoT?

Usually, IoT devices have limited computing power (raspberry pi and other next-gen IoT devices would be the exception) and therefore efficiency is of the uttermost importance. To achieve this the MQTT protocol was created as a low-overhead protocol that could provide reliable and effective communication with the bandwidth and the CPU limitations of the devices in mind. Since the MQTT broker buffers unsent messages, this protocol can be used over unreliable, wireless connections. This is very useful for home assistance telemetry devices.

WHAT IS A MQTT BROKER?

An MQTT broker is an intermediary between devices and is the only one in charge of delivering the messages to the subscribing devices. In a real-world example, we can think of an MQTT Broker as a post office. We deliver a letter to the post office and then the post office is in charge of delivering that letter to whom it may concern.

There are multiple MQTT brokers out there that you can use out of the box but in case you are feeling spicy, you could even make your own broker as long as you study the MQTT and implement it accordingly. Later on this article, we will talk about the Eclipse’s MQTT Broker — Eclipse Mosquitto™

TYPES OF MQTT MESSAGES

I’ll list the existing MQTT messages but since these are defined by the protocol and in most cases you will be using publicly available libraries to implement an MQTT solution for your project there is no need to dive too deep into this topic.

CONNECT — Is the client request to connect to the broker

CONNACK — Acknowledgement of the connect

PUBLISH — Publishes a message to a topic

PUBACK — Acknowledgement of the publish with QoS level 1

PUBREC — Acknowledgement of the publish with QoS level 2 (2nd packet)

PUBREL — Response to the PUBREC. (3rd packet when using QoS level 2)

PUBCOMP — Response to PUBREL (4th and last packet when using QoS lvl 2)

SUBSCRIBE — Packet from the client to subscribe to topics

SUBACK — Acknowledgement of the subscribe packet

UNSUBSCRIBE — Packet from the client to unsubscribe from topics

COMMONS USAGES OF MQTT

The MQTT protocol is mainly applied in remote monitoring such as fire detectors, temperature readers, motion detection, health monitoring, sensors and perhaps the most surprising one of all is Facebook’s Messanger.

SETTING UP A MOSQUITTO BROKER

  1. Download and install Mosquitto from the official website (https://mosquitto.org/download/). If you are using Mac or Linux they also have there the commands that you have to run.
  2. Download and install MQTT Explorer (http://mqtt-explorer.com/). This step is optional and should only be used to test the connection.
  3. If you are using Windows, you can open the services tab and check that the Mosquitto Broker service is created. Run if it is not running already.

4. Open the MQTT Explorer we installed previously and let's connect to our broker. Since it is a fresh install we do not need to use a username and password. Simply type localhost on the Name textbox and press connect.

5. Once you connect you will see a new window. In this new window, you can check what messages you are receiving and you can also send messages yourself:

Highlighted in red is where write your message and send it to the broker. Highlighted in blue is where received messages are displayed.

the MQTT Explorer application automatically subscribes to # which means that that client is now subscribed to all topics except the ones starting with $ (those are reserved for system stuff).

On the image above, inside the red square let’s now attempt to send a message to the broker. Type the name of the topic that you want to send it to (don’t worry about it ‘not existing’ yet because you don’t need to manage that) and some message, in one of the forms: raw text, XML, or JSON.

Publishing a JSON message to topic ‘test’

Once you hit ‘publish’ you’ll see on the left side that your message has hit the broker.

This will be it for a very basic usage of the mosquitto broker, on the next topic we’ll talk a bit about the mosquitto broker configurations.

CONFIGURING YOUR MOSQUITTO BROKER

First, let’s create a password file. Name it something like passwd.txt and for simplicity’s sake place it in your mosquitto installation folder. By default on Windows, it should be something like ‘c:/mosquitto’.

Open your newly created file. In this file, you can consider a line as a single user/pass combination (the format of the line has to be <username>:<password>). Let’s create our first user by typing: test:testpw.

Hit save and close the text editor.

Now to encrypt our user’s password open a terminal/command line prompt and change directory to your and type the following command and hit Enter:

mosquitto_passwd.exe -U passwd.txt

Your password file will be replaced with another file that has all passwords encrypted.

Now we need to tell the broker to use our password file and to do this open the mosquitto.conf file located in the mosquitto broker installation folder.

After opening it, search for the fields ‘allow_anonymous’ and set it to false.

allow_anonymous false

Search for the field ‘password_file’ and set it your passwd.txt file.

password_file c:\mosquitto\passwd.txt

Search for the field ‘per_listener_settings’ and set it to true.

per_listener_settings true

Now save the .conf file and close it.

If you try to connect to your broker now, you will see that you can still connect without a password, this is because we need to restart the broker for it to read the new settings that we changed.

After restarting the broker, the broker should require a username and password when a client is trying to connect.

Allowing connections from clients outside of our machine

When trying to send messages to the broker you might have noticed that when you connect to the broker from a different machine than the one where the broker was installed that you were unable to do so. This is due to the operating system not having the needed ports configured. To change this we have to create an inbound rule for mosquitto broker’s TCP port (by default it is port 1883 but you can change it in the .conf file).

On Windows, open your Windows Defender Firewall (you can get to it by going to Control Panel > Change to large icons > Windows Defender Firewall)

Go to Advanced settings

Click on ‘Inbound Rules’ and then on ‘New Rule…’

On the ‘New Inbound Rule Wizard” window, select ‘Port’ and hit next.

Select ‘TCP’ and ‘Specific local port’, on the ports’ textbox type 1883 (or your mosquitto broker port if you changed it yourself in the .conf file). Hit next once you are done.

Now select ‘Allow the connection’ and hit next.

Now select to what domains that this rule applies to (Domain, private or public). You can select all but this is not recommended in a production environment. Hit next once you are done and finally type a name for your new rule. Name it something like ‘MQTT Broker Port’ and hit Finish.

Now your rule is created and you should be able to connect from another machine in the same network.

CONCLUSION

In conclusion, the MQTT protocol is a versatile messaging protocol that can be used in all different types of industries. Is light enough to be used by IoT devices but still powerful enough for it to be used by giant corporations, like Facebook.

I hope you enjoyed the reading and that my guide helped you configure your first broker. There are a lot more configurations that you can do, like setting up SSL/TLS but this article was becoming very extensive so I decided to not include it here but maybe in the future, I’ll do a second part where we can dive deeper into the configurations of the mosquitto broker.

This was my first article ever, I’ve enjoyed writing it very much but I am aware that there is much room for improvement so I would like to apologize if it is not up to par.

--

--