In recent post, we learned how to run mosquitto as a docker container. Now, we will learn about how to carry out messages using its publish/subscribe model.
In order to publish/subscribe to a topic in mosquitto, we need mosquitto-clients to be installed in our machine. We can do it by the following command:
1 | $ sudo apt-get install mosquitto-clients |
This will make both mosquitto_pub and mosquitto_sub available to use.
Publish message under a topic
In MQTT protocol, messages should be published and subscribed under respective topic names. For instance, if we are going to publish a message related to weather, it should be published like the following: /topic/weather/nl/utrecht.
Lets do the same, using mosquitto_pub:
1 | $ mosquitto_pub -t '/topic/weather/nl/utrecht' -m '{ "city":"Utrecht", "temp": "20" }' |
-t– The topic under which we publish a message.-m– The message we want to publish.
mosquitto_pub publishes the messages under the specified topic via default port 1883 of localhost.
Tip: We can publish anything as long as we keep it as a string. The above is just a string representation of a
WeatherJSON object. People subscribe to it, can parse it usingJSON.parse()to use as a JSON.
Subscribe to a topic
Since people publishing already, lets subscribe to a topic and hear the gossip O_o
1 | $ mosquitto_sub -t '/topic/weather/nl/utrecht' |
If there are messages being published, you can see it being logged in your terminal.
Tip: We can subscribe to all the topics by specifying the
-toption as the following:mosquitto_sub -t '#'. However, it is advised to subscribe to as specifici topic as possible likemosquitto_sub -t '/topic/weather/nl/#'to subscribe to the entire country’s weather.
1 | $ mosquitto_sub -t '/topic/weather/nl/#' |