In this post you’re going to test the Mosquitto Broker and Client on a Raspberry Pi by subscribing to an MQTT topic and publishing sample messages.

Recommended resources:
- You need a Raspberry Pi board – read Best Raspberry Pi Starter Kits
- How to Install Mosquitto Broker on Raspberry Pi
- What is MQTT and How It Works
- Getting Started with Node-RED on Raspberry Pi
Testing MQTT Broker Installation
After installing MQTT Broker, I recommend installing an MQTT Cllient to test the Broker installation and publish sample messages.
The next command shows how to install MQTT Mosquitto Client:
pi@raspberry:~ $ sudo apt-get install mosquitto-clients
You’ll have to type Y and press Enter to confirm the installation.
Run Mosquitto on background as a daemon:
pi@raspberry:~ $ mosquitto -d
Subscribing to testTopic Topic
To subscribe to an MQTT topic with Mosquitto Client open a terminal Window #1 and enter the command:
pi@raspberry:~ $ mosquitto_sub -d -t testTopic

You’re now subscribed to a topic called testTopic.
Publishing “Hello World!” Message to testTopic Topic
To publish a sample message to testTopic, open a terminal Window #2 and run this command:
pi@raspberry:~ $ mosquitto_pub -d -t testTopic -m "Hello world!"

The message “Hello World!” is received in Window #1 as illustrated in the figure above.
Publishing a Message to Multiple Clients
Having Window #1 still subscribed to topic testTopic, open a new terminal Window #3 and run this command to subscribe to testTopic topic:
pi@raspberry:~ $ mosquitto_sub -d -t testTopic
On Window #2 publish the “Hello World!” message:
pi@raspberry:~ $ mosquitto_pub -d -t testTopic -m "Hello world!"

Since two clients are subscribed to testTopic topic, they will both receive “Hello world!” message.
This simple example shows how MQTT works and how your devices (for example: ESP8266) could be subscribed to the same topic to receive messages or a device could publish messages to multiple devices. We’ll explore this concept further in future blog posts.

7 thoughts on “Testing Mosquitto Broker and Client on Raspbbery Pi”