Publish
mosquitto_pub -t <topic> -m <msg>
Publish one message
mosquitto_pub -t <topic> -f <file>
Send a file as the payload
mosquitto_pub -t <topic> -s < <file>
Send all of stdin as one message
<cmd> | mosquitto_pub -t <topic> -l
Send each stdin line as a message
mosquitto_pub -t <topic> -n
Send an empty payload
With -r this clears the retained message on the topic.
mosquitto_pub -r -t <topic> -m <msg>
Retain message on the broker
New subscribers get the last retained value immediately.
mosquitto_pub -q <0|1|2> -t <topic> -m
<msg>
Set delivery guarantee
Default is 0 (at most once); 2 is exactly once.
mosquitto_pub --repeat <n> --repeat-delay <s>
-t <topic> -m <msg>
Publish repeatedly
Subscribe
mosquitto_sub -t <topic>
Print each payload
mosquitto_sub -v -t <topic>
Prefix payloads with the topic
mosquitto_sub -t <a> -t <b>
Subscribe to several filters
mosquitto_sub -q <0|1|2> -t <topic>
Set subscription QoS
mosquitto_sub -C <n> -t <topic>
Exit after n messages
mosquitto_sub -W <s> -t <topic>
Exit after s seconds
Exit status 27 means it timed out waiting.
mosquitto_sub -R -t <topic>
Skip retained messages
mosquitto_sub --retained-only -t <topic>
Only retained messages, then exit
mosquitto_sub -T <topic> -t <filter>
Hide matching topics from output
Unsubscribing with -U cannot punch holes in a wildcard.
mosquitto_sub -F "%t %p" -t <topic>
Custom output format
%t topic, %p payload, %x hex payload, %r retain flag, %q QoS,
%l length, @Y-@m-@d... timestamps.
mosquitto_sub -E -t <topic>
Exit once subscribed
mosquitto_sub -t <topic> --remove-retained
--retained-only
Delete retained messages
Run it on a narrow filter first; '#' clears
everything.
Connection & auth
-h <host> -p <port>
Broker address (localhost:1883)
-u <user> -P <password>
Username and password
-P shows in the process list and shell history.
-i <id>
Fixed client ID
Two clients with the same ID kick each other off.
-I <prefix>
Client ID prefix plus PID
-L mqtt://<url>
Everything in one URL
Form: mqtt://user:pw@host:port/topic.
-V <5|311|31>
MQTT protocol version
Default is 3.1.1.
-c -i <id>
Persistent session
Needs -i; the broker queues QoS 1/2 messages while you are
offline.
-k <seconds>
Keepalive interval
--ws
Connect over WebSockets
-d
Print protocol debug output
--will-topic <topic> --will-payload <msg>
Last will on unexpected disconnect
Add --will-retain and --will-qos as needed.
Users & passwords
mosquitto_passwd -c <file> <user>
Create file with a user
-c overwrites an existing file.
mosquitto_passwd <file> <user>
Add a user or change password
mosquitto_passwd -b <file> <user>
<password>
Set password non-interactively
Password lands in shell history.
mosquitto_passwd -D <file> <user>
Delete a user
mosquitto_passwd -U <file>
Hash a plain-text password file
Never run it on an already hashed file.
mosquitto_passwd -H sha512 <file> <user>
Hash for Mosquitto 1.6 and older