LinuxCommandLibrary

mosquitto_sub

Subscribe to MQTT topics

TLDR

Subscribe to the topic sensors/temperature information with Quality of Service (QoS) set to 1. (The default hostname is localhost and port 1883)

$ mosquitto_sub [[-t|--topic]] [sensors/temperature] [[-q|--qos]] [1]
copy

Subscribe to all broker status messages publishing on iot.eclipse.org port 1885 and print published messages verbosely
$ mosquitto_sub [[-v|--verbose]] [[-h|--host]] "iot.eclipse.org" [[-p|--port]] 1885 [[-t|--topic]] [\$SYS/#]
copy

Subscribe to multiple topics matching a given pattern. (+ takes any metric name)
$ mosquitto_sub [[-t|--topic]] [sensors/machines/+/temperature/+]
copy

SYNOPSIS

mosquitto_sub [-h host] [-p port] [-q qos] [-i clientid] [-t topic] [-u username] [-P password] [-d] [-c] [-k keepalive] [-W timeout] [-L URL] [--will-topic topic] [--will-payload payload] [--will-qos qos] [--will-retain] [-v] [--disable-clean-session] [--tls-version tls-version] [--tls-insecure] [--cert certfile] [--key keyfile] [--cafile cafile] [--capath capath] [--psk psk] [--psk-identity identity] [--proxy socks-url] [-F format]

PARAMETERS

-h host
    The hostname of the MQTT broker to connect to. Defaults to 'localhost'.

-p port
    The port number of the MQTT broker. Defaults to 1883.

-q qos
    The Quality of Service level for the subscription (0, 1, or 2). Defaults to 0.

-i clientid
    The client ID to use. If not specified, a random client ID is generated.

-t topic
    The topic to subscribe to. Can be specified multiple times to subscribe to multiple topics.

-u username
    The username to use for authentication.

-P password
    The password to use for authentication.

-d
    Enable debug mode. Prints more verbose output.

-c
    Disable 'clean session' (persistent subscriptions). If not set, the broker will discard the subscriber's subscriptions after the subscriber disconnects. Note: This is now deprecated and --disable-clean-session should be used instead.

-k keepalive
    The keep alive interval in seconds. Defaults to 60.

-W timeout
    The connection timeout in seconds. Defaults to no timeout.

-L URL
    Specify the broker connection details with a URL. e.g., mqtt://user:password@host:port

--will-topic topic
    The topic to publish a will message to if the client disconnects unexpectedly.

--will-payload payload
    The payload of the will message.

--will-qos qos
    The QoS level for the will message.

--will-retain
    Set the retain flag for the will message.

-v
    Enable verbose output. Prints the topic of each received message.

--disable-clean-session
    Disable 'clean session' (persistent subscriptions). The broker will store subscriptions for a disconnected client, enabling offline messages to be delivered upon reconnection.

--tls-version tls-version
    The TLS protocol version to use (e.g., tlsv1.2, tlsv1.1, tlsv1).

--tls-insecure
    Disable TLS certificate verification. Should only be used for testing!

--cert certfile
    The client certificate file to use for TLS authentication.

--key keyfile
    The client private key file to use for TLS authentication.

--cafile cafile
    The CA certificate file to use for verifying the server certificate.

--capath capath
    The directory containing CA certificate files.

--psk psk
    The pre-shared key to use for TLS-PSK authentication.

--psk-identity identity
    The pre-shared key identity to use for TLS-PSK authentication.

--proxy socks-url
    SOCKS5 proxy details, e.g., socks5://user:password@host:port

-F format
    Format the output according to the format string. Useful for parsing data. e.g., -F '%t %p' will print the topic and payload separated by a space.

DESCRIPTION

The mosquitto_sub command is a simple MQTT client that subscribes to topics and prints the messages it receives. It's part of the mosquitto suite of tools. It is primarily used for testing and debugging MQTT brokers and clients. The command connects to an MQTT broker, specified by hostname and port, subscribes to one or more topics, and then continuously listens for messages published on those topics. The received messages are then printed to standard output. The tool supports various MQTT protocol versions and can handle TLS/SSL encrypted connections for secure communication. It provides options for setting the QoS (Quality of Service) level for the subscription, specifying client IDs, providing usernames and passwords for authentication, and configuring keep-alive intervals.
mosquitto_sub can be used in scripting environments to automate tasks that rely on receiving MQTT messages. It's a valuable tool for understanding and troubleshooting MQTT communication.

WILDCARDS

The mosquitto_sub command supports wildcards in topic subscriptions.
'#' (hash) is a multi-level wildcard that matches all topics recursively.
'+' (plus) is a single-level wildcard that matches only one level in the topic hierarchy.
Example: 'sensor/+/temperature' would match 'sensor/room1/temperature', 'sensor/room2/temperature', but not 'sensor/room1/basement/temperature'.

HISTORY

mosquitto_sub is part of the Mosquitto project, an open-source MQTT broker and client library. The Mosquitto project was started by Roger Light to provide a lightweight and reliable MQTT implementation. Mosquitto tools are widely used for building IoT and messaging applications. The tool has evolved since its initial release with added features and improvements to meet the demands of modern MQTT deployments.

SEE ALSO

Copied to clipboard