LinuxCommandLibrary

mosquitto_pub

Publish MQTT messages to a broker

TLDR

Publish a temperature value of 32 on the topic sensors/temperature to 192.168.1.1 (defaults to localhost) with Quality of Service (QoS) set to 1

$ mosquitto_pub [[-h|--host]] [192.168.1.1] [[-t|--topic]] [sensors/temperature] [[-m|--message]] [32] [[-q|--qos]] [1]
copy

Publish timestamp and temperature data on the topic sensors/temperature to a remote host on a non-standard port
$ mosquitto_pub [[-h|--host]] [192.168.1.1] [[-p|--port]] [1885] [[-t|--topic]] [sensors/temperature] [[-m|--message]] "[1266193804 32]"
copy

Publish light switch status and retain the message on the topic switches/kitchen_lights/status to a remote host because there may be a long period of time between light switch events
$ mosquitto_pub [[-r|--retain]] [[-h|--host]] "[iot.eclipse.org]" [[-t|--topic]] [switches/kitchen_lights/status] [[-m|--message]] "[on]"
copy

Send the contents of a file (data.txt) as a message and publish it to sensors/temperature topic
$ mosquitto_pub [[-t|--topic]] [sensors/temperature] [[-f|--file]] [data.txt]
copy

Send the contents of a file (data.txt), by reading from stdin and send the entire input as a message and publish it to sensors/temperature topic
$ mosquitto_pub [[-t|--topic]] [sensors/temperature] [[-s|--stdin-file]] < [data.txt]
copy

Read newline delimited data from stdin as a message and publish it to sensors/temperature topic
$ [echo data.txt] | mosquitto_pub [[-t|--topic]] [sensors/temperature] [[-l|--stdin-line]]
copy

SYNOPSIS

mosquitto_pub [-h host] [-p port] [-q qos] [-r] [-t topic] [-m message] [-f filename] [-i clientid] [-u username] [-P password] [-d] [--will-topic topic] [--will-payload payload] [--will-qos qos] [--will-retain] [-s] [-k keepalive] [-c] [--tls-version tls-version] [--cafile file] [--capath dir] [--cert file] [--key file] [--psk hex key] [--id-prefix prefix]

PARAMETERS

-h host
    The hostname or IP address of the MQTT broker. Defaults to 'localhost'.

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

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

-r
    Retain the message. The broker will store the last message published on the topic and deliver it to new subscribers.

-t topic
    The MQTT topic to publish the message to. This is a mandatory parameter.

-m message
    The message payload to send. If not specified, the message will be read from standard input.

-f filename
    Read the message payload from a file.

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

-u username
    The username for authentication.

-P password
    The password for authentication.

-d
    Enable debug messages.

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

--will-payload payload
    The payload for the will message.

--will-qos qos
    The QoS level for the will message (0, 1, or 2).

--will-retain
    Retain the will message.

-s
    Use TLS for secure connection. Requires setting up certificates.

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

-c
    Disable 'clean session' flag. When used, the broker will store subscriptions and pending messages for a disconnected client.

--tls-version tls-version
    The TLS protocol version to use. Can be tlsv1.2, tlsv1.1, or tlsv1.

--cafile file
    Path to the certificate authority file.

--capath dir
    Path to a directory containing certificate authority files.

--cert file
    Path to the client certificate file.

--key file
    Path to the client private key file.

--psk hex key
    Pre-shared key (PSK) in hexadecimal format for TLS authentication.

--id-prefix prefix
    A prefix to be added to the client id, if one is not manually specified. Useful for generating unique client IDs

DESCRIPTION

mosquitto_pub is a command-line utility that allows you to publish MQTT messages from a Linux terminal. It's a lightweight and versatile tool for interacting with MQTT brokers, enabling you to send data to specific topics.

It's commonly used for testing MQTT infrastructure, sending sensor data, controlling IoT devices, and more. mosquitto_pub is part of the mosquitto suite, a popular open-source MQTT broker implementation. It provides various options to customize message delivery, including setting QoS levels, retaining messages, and specifying the message payload using files or directly from the command line.

The command is easy to integrate into shell scripts and automation workflows, making it a valuable asset for developers and system administrators working with MQTT-based applications. Whether you're building a complex IoT system or simply need to send a quick test message, mosquitto_pub provides a convenient and efficient way to interact with your MQTT broker.

CAVEATS

Ensure that the MQTT broker is running and accessible from the machine where mosquitto_pub is executed. Pay attention to TLS configuration if using secure connections.

MESSAGE PAYLOAD

If neither '-m' nor '-f' is specified, mosquitto_pub will read the message payload from standard input (stdin). This allows for piping data to the command. When using standard input, mosquitto_pub will continue reading until EOF is reached (e.g., by pressing Ctrl+D).

RETURN CODES

When running successfully, the program returns 0. If it returns an error, such as connection or protocol problems, it will return a different value.

HISTORY

mosquitto_pub is part of the Mosquitto project, which was created by Roger Light. It has become one of the standard client tools for working with MQTT brokers. It is widely used for testing, development, and deployment of MQTT solutions since early 2010s.

SEE ALSO

Copied to clipboard