Brief Explanation of MQTT Broker with Mosquitto

Alihan Doğuş Yalçın
5 min readApr 10, 2020

--

MQTT (Message Queuing Telemetry Transport) is a publish/subscribe (pub/sub) based messaging protocol for machine-to-machine (M2M) communication. It is a lightweight solution compared to other messaging protocols like AMQP or DDS. It works on devices with low CPU power and battery, and network with low bandwidth. Because of these low features, MQTT is typically used as a standard in the communication of IoT (Internet of Things) devices.

The key part of MQTT communication is software called the broker (MQTT Server). The MQTT broker is mainly responsible for the distribution of messages arrived from clients (Clients are devices that connect to the broker.). MQTT broker uses an address space named topic to deliver a message to a certain client. As you see in Figure 1, there are two clients that connect to the broker. One of the clients subscribes to the topic ‘X’ and the other one publishes a message with the topic ‘X’. There is no direct connection between clients and even the clients do not know anything about each other. The broker sends a copy of the received message to other clients that subscribe to the same topic.

Figure 1: One-To-One

If more than one client subscribes to the same topic, the broker sends messages published from the topic to all subscribers (Figure 2).

Figure 2: One-To-Many

Also, a client that stores all data can subscribe to multiple topics (Figure 3), and MQTT is a bi-directional communication protocol, in other words, all clients can publish and subscribe at the same time.

MQTT has three Quality of Service (QoS) variables that can be used in the importance of message delivery.
0 (at most once): Broker sends the message, and it does not wait for an acknowledgment. The message may be lost or duplicated.
1 (at least once): Broker sends the message, and it waits for an acknowledgment. The message may be duplicated, but the message delivery is under guarantee.
2 (exactly once): Broker guarantees that the message delivered to the client exactly once. No duplication or loss message.

Figure 3: Many-To-One

As is seen, scaling the communication environment from one device to a thousand devices can be facilitated using a broker. In addition to this, the broker provides security support with TLS (Transport Layer Security) encryption and credentials (username/password). The broker can be run on a personal computer (Mosquitto) or in the cloud (HiveMQ, CloudMQTT). Now, I will give some usage examples of Mosquitto to explain the MQTT broker better. (All steps following are for Linux based operating system.)

  1. Open a terminal and install mosquitto (Installation commands below are for Debian based distros):
sudo apt-get update
sudo apt-get install -y mosquitto
sudo apt-get install -y mosquitto-clients # for testing the broker

2. Check mosquitto is running, and if running, stop it for now:

systemctl status mosquitto # check mosquitto is running
sudo systemctl stop mosquitto # stop mosquitto

3. Now, go to /etc/mosquitto directory and list the directory contents. mosquitto.conf file is default mosquitto configuration file.

cd /etc/mosquitto/ && ls

5. Let’s create a new configuration file.

sudo nano example.conf

And paste port 1883, then, save the file and exit. Port 1883 is the default port for MQTT. Now, start mosquitto with our new configuration file.

mosquitto -c example.conf

If no error occurred, mosquitto starts to serve on port 1883. Launch two terminal to test the broker.

  • Subscribe to test topic in the first terminal.
mosquitto_sub -h 127.0.0.1 -t test
  • Publish a message to test topic in the second terminal.
mosquitto_pub -h 127.0.0.1 -t test -m "hello broker"

Turn back to the first terminal and check your new message. If everything is okay, let’s move on.

6. Another feature of MQTT broker is the persistent session. The broker can store all subscriptions and messages, so if a client loses the connection due to network errors or the broker restarts, the client can reconnect to the broker easily and the broker also will remember all the subscriptions of the clients. To activate persistent session on mosquitto, append the lines below to your configuration file example.conf. Then, restart the broker as a superuser sudo mosquitto -c example.conf . The data will be stored in mosquitto.db when the broker stops.

persistence true
persistence_file mosquitto.db
persistence_location /var/lib/mosquitto/
Figure 4: Retained message

7. The last thing I will mention in this story is retained messages (Figure 4). Retained messages are a great feature for real-world applications. For example, a client publishes a message with the topic ‘X’ every 2 hours. An hour after the first message, a new client subscribes to the topic ‘X’. Under normal circumstances, the new client has to wait an hour to get a message. But, the broker can store the last message received from a topic using retained message mode and if a new client subscribes to the topic, the broker publishes the last message. To test it, publish a message using mosquitto_pub with -r parameter.

mosquitto_pub -h 127.0.0.1 -t test -m "hello broker" -r

Then, subscribe to the topic.

mosquitto_sub -h 127.0.0.1 -t test

You got the message. Even the broker restarts, it will send you the last message because we activated the persistence session.

If you want to learn more about MQTT, visit this link. Also, you can visit mosquitto manual page to learn how you can configure your broker.

Thank you for your time.

--

--