Link Search Menu Expand Document Edit GitHub

Publish


Publishes a message to one or more topics.

mqtt publish

Alias: mqtt pub


Simple Examples

CommandExplanation
mqtt pub -t test -m "Hello"Publish the message Hello with topic ‘test’ using the default settings.
mqtt pub -t test1 -t test2 -m "Hello Tests"Publish the message Hello Tests with topics ‘test1’ and ‘test2’.
mqtt pub -t test -m "Hello" -h localhost -p 1884Publish the message Hello with topic ‘test’ to a broker at localhost:1884.
mqtt pub -t test -m:file payload.txtPublish the message in payload.txt with topic ‘test’ using the default settings.

Options

Publish options

OptionLong VersionExplanationDefault
-t--topicThe MQTT topic to which the message will be published. 
-m--messageThe message which will be published on the topic. 
-m:file--message-fileThe file containing the payload which will be published on the topic. 
-m:empty--message-emptySets the message to an empty payload. 
-r--[no-]retainWhether the message will be retained.false
-q--qosDefine the quality of service level. If only one QoS is specified it will be used for all topics.
You can define a specific QoS level for every topic. The corresponding QoS levels will be matched in order to the given topics.
0
-e--messageExpiryIntervalThe lifetime of the publish message in seconds. 
-ct--contentTypeA description of the content of the publish message. 
-cd--correlationDataThe correlation data of the publish message. 
-pf--payloadFormatIndicatorThe payload format indicator of the publish message. 
-rt--responseTopicThe topic name for the response message of the publish message. 
-up--userPropertyA user property of the publish message. 

Connect Options

OptionLong VersionExplanationDefault
-h--hostThe MQTT host.localhost
-p--portThe MQTT port.1883
-V--mqttVersionThe MQTT version can be set to 3 or 5.5
-i--identifierA unique client identifier can be defined.A randomly generated UTF-8 String.
-ip--identifierPrefixThe prefix for randomly generated client identifiers, if no identifier is given.mqttClient
-c--[no-]cleanStartWhether the client should start a clean session.true
k--keepAliveThe keep alive of the client (in seconds).60
-se--sessionExpiryIntervalThe session expiry value in seconds.0 (Instant Expiry)
-Cup--connectUserPropertyA user property of the connect message. 
-ws Use WebSocket transport protocol.false
-ws:path The path to the WebSocket located at the given broker host. 

Will Options

OptionLong VersionExplanationDefault
-Wd--willDelayIntervalWill delay interval in seconds.0
-We--willMessageExpiryIntervalLifetime of the will message in seconds.
Can be disabled by setting it to 4_294_967_295.
4_294_967_295 (Disabled)
-Wm--willPayloadPayload of the will message. 
-Wq--willQualityOfServiceQoS level of the will message.0
-Wr--willRetainRetain the will message.false
-Wt--willTopicTopic of the will message. 
-Wcd--willCorrelationDataCorrelation data of the will message. 
-Wct--willContentTypeDescription of the will message’s content. 
-Wpf--willPayloadFormatIndicatorPayload format can be explicitly specified as UTF8 else it may be UNSPECIFIED. 
-Wrt--willResponseTopicTopic name for a response message. 
-Wup--willUserPropertiesA user property of the will message. 

Connect Restrictions

OptionLong VersionExplanationDefault
 --rcvMaxThe maximum amount of not acknowledged publishes with QoS 1 or 2 the client accepts from the server concurrently.65535
 --sendMaxThe maximum amount of not acknowledged publishes with QoS 1 or 2 the client sends to the server concurrently.65535
 --maxPacketSizeThe maximum packet size the client accepts from the server.268435460
 --sendMaxPacketSizeThe maximum packet size the client sends to the server.268435460
 --topicAliasMaxThe maximum amount of topic aliases the client accepts from the server.0
 --sendTopicAliasMaxThe maximum amount of topic aliases the client sends to the server.16
 --[no-]reqProblemInfoThe client requests problem information from the server.true
 --reqResponseInfoThe client requests response information from the server.false

Security Options

Credentials Authentication

OptionLong VersionExplanationDefault
-u--userDefine the username for authentication. 
-pw--passwordDefine the password for authentication directly.
If left blank the user will be prompted for the password in the console.
 
-pw:env Define that the password for authentication is read in from an environment variable.MQTT_CLI_PW if the option is specified without a value
-pw:file Define the path to a file from which the password is read from. 

TLS Authentication

OptionLong VersionExplanationDefault
-s--secureWhether a default (properties file) TLS configuration is used.false
 --cafile, --ca-cert, --server-certThe path to the file containing a trusted CA certificate, to enable encrypted certificate based communication. 
 --capath, --ca-cert-dir, --server-cert-dirThe path to the directory containing trusted CA certificates, to enable encrypted certificate based communication. 
 --cert, --client-certThe path to the client certificate to use for client-side authentication. 
 --key, --client-private-keyThe path to the corresponding private key for the given client certificate. 
 --keypw, --client-private-key-passwordThe password for the client private key. 
 --ks, --keystoreThe path to the client keystore for client side authentication. 
 --kspw, --keystore-passwordThe password for the keystore. 
 --kspkpw, --keystore-private-key-passwordThe password for the private key inside the keystore. 
 --ts, --truststoreThe path to the client truststore, to enable encrypted certificate based communication. 
 --tspw, --truststore-passwordThe password for the truststore. 
 --ciphersThe supported cipher suites in IANA string format concatenated by the ‘:’ character, if more than one cipher should be supported.
e.g TLS_CIPHER_1:TLS_CIPHER_2
See IANA Format for supported cipher suite strings.
 
 --tls-versionThe TLS version to use - TLSv1.1 TLSv1.2 TLSv1.3.TLSv1.2

Logging Options

OptionLong VersionExplanationDefault
-l Log to ~./mqtt-cli/logs (Configurable through ~/.mqtt-cli/config.properties).false

Debug Options

OptionLong VersionExplanationDefault
-d--debugPrint debug messages to the console.false
-v--verbosePrint trace messages to the console.false

Help Options

OptionLong VersionExplanation
 --helpDisplay help message for command.
 --versionDisplay version information of the cli.

Further Examples

Publish a message with QoS level Exactly Once

NOTE: If you specify one QoS and multiple topics, the QoS will be used for all topics.

mqtt pub -t topic1 -t topic2 -q 2

Publish a message with a specific QoS for each topic. (‘topic1’ will have QoS 0, ‘topic2’ QoS 1 and ‘topic3’ QoS 2)

mqtt pub -t topic1 -q 0 -t topic2 -q 1 -t topic3 -q 2

Table of contents