Using The Mosquitto_pub and Mosquitto_sub MQTT Client Tools- Examples

The Mosquitto_pub and Mosquitto_sub client utilities comes with the Mosquitto package, and are excellent tools for conducting quick tests and troubleshooting..

In this tutorial we are going to be looking at some examples of using these tools for publishing and subscribing.



Publishing Using The Mosquitto_pub Client

The screen shot shot below shows a simple publish, and a publish with the debug flag (-d) set.

using-mosquito_pub-client

In the first example the message is published and the client exits without displaying any messages.

If you enable the debugging using the -d flag then you can see the connect,publish and disconnect messages.

Notice the -h flag sets the host name or IP address. To get help use the –help flag.

e.g. mosquitto_pub –help

Useful Flag Options and Examples

-r  Sets retain flag
-n  Sends Null message useful for clearing retain message.
-p – Set Port number Default is 1883
-u – Provide a username
-P – Provide a password
-i – Provide client name
-I – Provide a client id prefix- Used when testing client restrictions using prefix security.

Publish With Username and Password

Uses -u username and -P password flags

mosquitto_pub-example-1

Publish with Retain Flag

Uses -r Flag

mosquitto_pub-example-2

Clear Retained Message

Uses the -r -n flag combination

mosquitto_pub-example-3

Publish with Client Name

This  option is useful when testing client restrictions using prefixes or client names with ACLs.

Uses -i option. You could also use the -I option if you just needed prefixes.

mosquitto_pub-example-4

Publishing JSON Data

JSON Data has a special format described here. When is comes to publishing with the mosquitto_pub client you need to escape the quotes so that they are included.

So don’t use {“status”:”off”} but instead use: {\”status\”:\”off\”}.

mosquitto-pub-json-data

Mosquiito_pub -Publish Using SSL

Note: you will need to copy the CA.crt file into the appropriate directory. Use the –insecure switch if the name on the certificate doesn’t match the actual broker name.

mosquitto_pub-ssl

Notes
There is also a option to publish a will message using the –will-topic flag but I couldn’t get this to work.

Using The Mosquitto_sub Client

This is useful for quickly monitoring a topic. Flag options are mostly the same as the mosquitto_pub client.

When you start this client it runs indefinitely until stopped using CTRL+C.

mosquitto-sub-client-example

A useful option is the -C flag which will disconnect after receiving a certain number of messages.

mosquitto-sub-client-example-2

Using the -v  (verbose option will show the topic name as well as the message.

mosquitto_sub-v

The debug option -d flag is useful when you want to see the messages flags like qos and retain flag.

mosquitto-sub-client-example-3

You can change the QOS when subscribing using the -q switch.

mosquitto-sub-qos

Notes:

  • I didn’t get the scripts to work on Windows XP -Error not a valid win32 application
  • On Windows 7 and 10 they gave an error regarding a missing msvcr100.dll file. I found the file on my system and copied it to the Mosquitto directory and they worked OK.
  • You can download my windows install package that contains all the files for running the mosquitto broker and these clients from here.

References:

Here is the pub manual page, and here is the sub manual page.

Summary

The mosquitto_pub and mosquitto_sub client utilities are excellent tools for testing and troubleshooting MQTT and broker problems.

Other Related Articles and Resources:

Facebooktwittergoogle_plusredditpinterestlinkedinmail

8 comments

  1. Hello Steve, thanks for this nice documentation. I ham having some problem with using mosquitto_pub with SSLCertificate. On the client side, when i use:
    mosquitto_pub -h 127.0.0.1 -t test -m “message” -d –cafile certs\ca.crt -p 8883 –insecure
    it shows the following error:
    Client mosqpub|13588-nb2166 sending CONNECT
    Error: A TLS error occurred.
    On the Broker side, it shows the following error:
    1534923031: New connection from 127.0.0.1 on port 8883.
    1534923031: Socket error on client , disconnecting.
    I have already tested the Broker with MQTTfx and there it works fine with certificate. Could you help me out please ?

      1. Yes that is true. it is the same path also added in mosquitto conf in the line cafile.
        and it also shows the following error on the broker side.
        1534933587: New connection from 127.0.0.1 on port 8883.
        1534933587: OpenSSL Error: error:14094418:SSL routines:ssl3_read_bytes:tlsv1 alert unknown ca
        1534933587: OpenSSL Error: error:140940E5:SSL routines:ssl3_read_bytes:ssl handshake failure
        1534933587: Socket error on client , disconnecting.

        Is it a problem to use a selfed signed certificate? How to solve this problem?

      2. Hello Steve, I found the issue. It was showing the error that the CA is unknown as i had put the same information in both the information in server and ca crt files. For other users, please do not forget to read the yellow note on this webpage or else you will end up like me. 😉
        http://www.steves-internet-guide.com/mosquitto-tls/
        In short if you get this error from OpenSSL, it means the certificates are not correct:
        1534938680: New connection from 127.0.0.1 on port 8883.
        1534938680: OpenSSL Error: error:14094418:SSL routines:ssl3_read_bytes:tlsv1 alert unknown ca
        1534938680: OpenSSL Error: error:140940E5:SSL routines:ssl3_read_bytes:ssl handshake failure
        1534938680: Socket error on client , disconnecting.
        Now it mosquitto_pub as well as libmosquitto are working correctly. THank you again.

  2. mosquitto_pub -h 127.0.0.1 -p 1883 -t test -m “Electroll”

    when using this command or any other commands with publish and subscribe the ting crashes ,
    receiving messages like “mosquitto_pub.exe stopped working” what should i do
    i also checked netstat -a , port 1883 is present

Leave a Reply

Your email address will not be published. Required fields are marked *