Appendix B. Mosquitto

In this appendix, we describe how to install the Mosquitto broker and its tools to produce and consume MQTT messages from the command line.

Download and Installation

Mosquitto is an open source message broker that implements the MQTT protocol. The public MQTT broker hosted on iot.eclipse.org that we used in Chapter 6 and Chapter 7 is an instance of Mosquitto.

Mosquitto also comes with two command-line tools to send and receive MQTT messages. These tools are handy when writing an application using MQTT to be able to quickly produce and consume messages and check that the application is working as expected.

To install Mosquitto on your operating system, please consult its documentation page, which contains instructions for various operating systems and package managers.

Produce Messages with mosquitto_pub

The mosquitto_pub command-line tool can be used to produce MQTT messages.

In this book, we use it in the simplest fashion to send a message. The parameters we must pass to the tool are:

  • -h host to specify the host of the MQTT broker

  • -t topic to specify the topic of the MQTT message

  • -m message to specify the message payload to send

For example, to send a message with the text yellow for payload on the topic /mwm/XYZ/alert on the broker hosted on iot.eclipse.org, the command line is:

$ mosquitto_pub -h iot.eclipse.org -t /mwm/XYZ/alert -m yellow

The full list of the mosquitto_pub parameters is available by calling mosquitto_pub --help:

 $ mosquitto_pub ...

Get Mobile and Web Messaging now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.