mosquitto_sub

subscribes to MQTT topics and prints received messages

TLDR

Subscribe to topic
$ mosquitto_sub -t [sensors/temperature]
Subscribe to all topics
$ mosquitto_sub -t '#'
Subscribe with wildcard
$ mosquitto_sub -t '[sensors/+/temperature]'
Connect to remote broker
$ mosquitto_sub -h [broker.example.com] -t [topic]
Subscribe with authentication
$ mosquitto_sub -h [broker] -u [username] -P [password] -t [topic]
Subscribe with TLS
$ mosquitto_sub -h [broker] --cafile [ca.crt] -t [topic]
Show verbose output
$ mosquitto_sub -v -t '[sensors/#]'
Read single message and exit
$ mosquitto_sub -t [topic] -C 1

SYNOPSIS

mosquitto_sub [-h host] [-p port] [-t topic] [-u user] [-P pass] [options]

DESCRIPTION

mosquitto_sub subscribes to MQTT topics and prints received messages. MQTT is a lightweight publish/subscribe messaging protocol for IoT and telemetry.Topics use hierarchical naming with / separators. Wildcards expand subscriptions: + matches one level, # matches all remaining levels. For example, sensors/+/temperature matches any sensor's temperature.Quality of Service levels control delivery guarantees: QoS 0 is fire-and-forget, QoS 1 ensures at-least-once delivery, QoS 2 guarantees exactly-once delivery.Retained messages are stored by the broker and sent immediately on subscription. This provides last-known values to new subscribers.TLS encryption protects communication. Client certificates enable mutual authentication. The broker must be configured to accept TLS connections.

PARAMETERS

-t TOPIC

Topic to subscribe to (can repeat).
-h HOST
Broker hostname.
-p PORT
Broker port (default 1883).
-u USER
Username.
-P PASSWORD
Password.
-i ID
Client ID.
-v
Verbose (print topic with message).
-C COUNT
Exit after receiving count messages.
-q QOS
Quality of service (0, 1, 2).
--cafile FILE
CA certificate for TLS.
--cert FILE
Client certificate.
--key FILE
Client private key.
-F FORMAT
Output format string.
-N
Don't append newline to messages.
--retained-only
Only show retained messages.

INSTALL

sudo apt install mosquitto-clients
sudo apk add mosquitto-clients
sudo zypper install mosquitto-clients

CAVEATS

Subscribing to # on busy brokers produces high volume. QoS 2 has significant overhead. Passwords on command line visible in process list.

HISTORY

mosquitto_sub is part of the Eclipse Mosquitto project, an MQTT broker and client suite. Mosquitto was created by Roger Light starting around 2009 and joined the Eclipse Foundation in 2014.

SEE ALSO

mosquitto_pub(1), mosquitto(8), mosquitto_passwd(1)