Start using your Mosquitto Ubuntu Appliance

After installing the Mosquitto image, the Mosquitto broker will be running with the default configuration, listening for connections on port 1883. To test the broker, you can use the mosquitto-pub and mosquitto-sub command-line utilities. This is a basic test example to start using your appliance. If you want more information about Mosquitto and what to do next the best place to go is the Mosquitto website.

mosquitto_pub allows you to publish messages to an MQTT broker, and mosquitto_sub allows you to subscribe to messages from an MQTT broker. Both tools have options to control how they are used so they are useful for a wide variety of tasks. In this case, we will use them for some simple testing.

To subscribe to all messages being published to the MQTT broker on the snap/example topic, run this command in your appliance:

mosquitto_sub -h localhost -t 'snap/example' -v

If your MQTT broker is not running on the same machine as mosquitto_sub, you will need to change the localhost argument to match your MQTT broker host or IP address.

The -t snap/example option sets the topic to subscribe and can be provided multiple times. The -v option means to print both the topic of the message as well as its payload. Now to publish a message to the same topic you use mosquitto_pub:

mosquitto_pub -h localhost -t 'snap/example' -m 'Hello from mosquitto_pub'

In this case, the -m option provides the message payload to be published. If everything works as planned, you should see mosquitto_sub print

snap/example Hello from mosquitto_pub

This is a very simple example but allows testing of the broker operation. Other things you may wish to try are subscribing to wildcard topics that include # or , or subscribing to the $SYS/# topic to see the information the broker is publishing about itself.

Note:

The command line treats # as a special character, and SSYS will be expanded as an environment variable if you do not surround them with single quotes.

Once you have finished your testing, you will want to configure your broker to have encrypted connections and use authentication, or possibly configuring bridges, which allow different brokers to share topics. or many other options. To do this, you need to provide a new configuration file. The snap provides an example configuration file at /var/snap/mosquitto/common/mosquitto_example.conf.

This file contains all of the broker configurations, in a similar manner to the man page. To create your own configuration, copy the example file to /var/snap/mosquitto/common/mosquitto.conf and edit according to your needs. Any additional files required by the configuration, such as TLS certificates and keys, must also be placed in /var/snap/mosquitto/common/ and have their full path provided in the configuration file.

{{ image( url="https://assets.ubuntu.com/v1/0fbfab25-mosquitto-text-side.png", alt="", width="400", height="81", hi_def=True, loading="lazy", ) | safe}}