Mosquitto is a popular MQTT (Message Queuing Telemetry Transport) broker, a lightweight and efficient messaging protocol ideal for the Internet of Things (IoT).
Mosquitto is an MQTT broker that facilitates communication between different devices and applications through messages. MQTT is a publish/subscribe messaging protocol that runs over TCP/IP, designed to be lightweight and efficient in data transmission.
Some key features of Mosquitto are,
- Lightweight and efficient: Ideal for resource-limited devices like the Raspberry Pi.
- Scalability: Can handle large volumes of messages and devices.
- Support for MQTT v3.1 and v3.1.1: Compatible with the most common versions of the MQTT protocol.
- Easy Configuration: Allows easy configuration of communication between devices.
If you don’t know what MQTT is or want to learn more about this widely used protocol in IoT, I leave this link here for you 👇.
Get to know MQTT, the popular communication protocol for IoT
Installing Mosquitto on Raspberry Pi
Before installing Mosquitto, we must ensure that our Raspberry Pi is up to date. We run the following commands to update packages and the operating system:
sudo apt update sudo apt upgrade
To install Mosquitto on Raspberry Pi, we simply use the apt package manager. Open a terminal and run the following command:
sudo apt install mosquitto mosquitto-clients
This command will install both the Mosquitto broker and the necessary client tools to interact with it.
You can check the status of the Mosquitto service with,
sudo systemctl status mosquitto
This will give you information about whether the broker is running or if it has encountered any problems.
Basic Configuration
Mosquitto comes with a default configuration that is suitable for getting started, but we can adjust it according to our needs. The main configuration file is located at /etc/mosquitto/mosquitto.conf.
To modify the configuration, open the file with a text editor using the following command:
sudo nano /etc/mosquitto/mosquitto.conf
In this file, we can configure various parameters, such as the port the broker will listen on (default is port 1883), as well as options for authentication and log files.
Configuring Authentication
If we want to enable authentication for accessing the Mosquitto broker, we first need to set up a password file. We start by installing the mosquitto_passwd tool:
sudo apt install mosquitto
Then, we create a password file with the following command:
sudo mosquitto_passwd -c /etc/mosquitto/pwfile username
Replace username with the desired username. You will be prompted to enter and confirm a password.
Once the password file is created, edit the configuration file /etc/mosquitto/mosquitto.conf to add the following line:
password_file /etc/mosquitto/pwfile
Save and close the file, and then restart Mosquitto to apply the changes with the following command:
sudo systemctl restart mosquitto
Using Mosquitto
Once we have installed and configured Mosquitto, we can start using it to send and receive MQTT messages.
Publishing a message
To publish a message on a specific topic, we use the mosquitto_pub command. For example, to send the message “Hello, MQTT!” to the topic “test/topic”, we run:
mosquitto_pub -h localhost -t “test/topic” -m “Hello, MQTT!”
Subscribing to a topic
To subscribe to a topic and receive messages, we use the mosquitto_sub command. For example, to subscribe to the topic “test/topic”, we run:
mosquitto_sub -h localhost -t “test/topic”
When we publish a message to the “test/topic” topic, we will see the message in the terminal where we are subscribed.

