I. Start mosquitto:
Here, I run the following command to launch Mosquitto with the default configuration file. On your device, the command could be different. In part 1 of this series, I showed you how to get the command, you should definitely check it out.
/opt/homebrew/opt/mosquitto/sbin/mosquitto -c /opt/homebrew/etc/mosquitto/mosquitto.conf
After running the launch command, you should see:
II. Subscribe to a topic:
Open a Terminal in macOS and run the following command:
mosquitto_sub -t MyTopic -d -h localhost
You should see
The previous command creates an MQTT client that establishes a connection with the local MQTT server (broker) and then makes the client subscribe to the topic specified after the -t
option: MyTopic. We specify the -d
option to enable debug messages that will allow us to understand what happens under the hoods (try running again without -d and compare the result).
Keep the window opened. You will see that that the client sends PINGREQ
packets to the MQTT server and receives PINQRESP
packets from the MQTT server. Examples of the messages displayed for these packets are shown as follows:
Optional: You can explore more flag options by running the command below in a new Terminal window:
mosquitto_sub --help
III. Understand the Commands:
MQTT is a connection-oriented protocol. Once a connection is established between the server and the client, commands are sent back and forth over the connection. The table below summarizes common commands and their description.
Now, try to understand the massages displayed when you run mosquitto_sub previously.
Brittany here! Let me know if you have any questions!