mosquitto_sub(1)
Subscribe to MQTT topics and receive published messages from a Mosquitto broker.
Synopsis
mosquitto_sub [OPTION]... -t TOPIC [TOPIC ...]Description
mosquitto_sub is a simple MQTT client that subscribes to one or more topics on a Mosquitto broker and prints all received messages to standard output. It connects to the specified broker, subscribes to the given topics using MQTT topic filters (which support wildcards), and displays incoming messages in real-time.
Topic names can include wildcards: the + character matches a single level, and # matches multiple levels. Multiple topics can be specified by using the -t flag multiple times. The client will stay connected until terminated or until an error occurs.
Common options
| Flag | What it does |
|---|---|
-h HOST | Hostname or IP address of the MQTT broker (default: localhost) |
-p PORT | Port number for the broker connection (default: 1883 for MQTT, 8883 for MQTT over TLS) |
-t TOPIC | MQTT topic(s) to subscribe to; use multiple -t for multiple topics |
-q QoS | Quality of Service level (0, 1, or 2; default: 0) |
-u USERNAME | Username for authentication with the broker |
-P PASSWORD | Password for authentication with the broker |
-v | Print topic name along with message payload |
-F FORMAT | Output format string with escape sequences like %t (topic), %m (message), %q (QoS) |
--cafile FILE | PEM file containing trusted CA certificates for TLS connections |
-i CLIENT_ID | Client ID to use when connecting to the broker (default: mosquitto_sub_PID) |
-k KEEPALIVE | Keep-alive interval in seconds (default: 60) |
-C MSG_COUNT | Exit after receiving the specified number of messages |
Examples
Subscribe to a single topic on a remote broker and print all messages
mosquitto_sub -h broker.example.com -t 'home/temperature'Subscribe to multiple topic patterns using wildcards to match all temperature and humidity sensors
mosquitto_sub -t 'home/+/temperature' -t 'home/+/humidity'Subscribe to all topics under 'sensors/' and print both topic name and message with -v flag
mosquitto_sub -h broker.example.com -t 'sensors/#' -vConnect to a TLS-enabled broker on a non-standard port with authentication
mosquitto_sub -h 192.168.1.100 -p 8883 -u myuser -P mypass -t 'status/device' --cafile ca.crtSubscribe with custom output formatting to show topic name and message with a temperature symbol
mosquitto_sub -t 'home/temperature' -F '%t: %m°C'Subscribe with QoS level 1 and exit after receiving 10 messages
mosquitto_sub -t 'alerts/#' -C 10 -q 1