Paho Python MQTT Client – Publish With Examples

publishing-messagesIn this tutorial we will look at how you publish messages using the Paho Python MQTT client.

We will use an example python script to publish messages, process the publish acknowledgements and examine QOS (quality of service) settings.

To publish a messages you use the publish method of the Paho MQTT Class object.

The publish method accepts 4 parameters. The parameters are shown below with their default values.

publish(topic, payload=None, qos=0, retain=False)

The only parameters you must supply are the topic, and the payload.

The payload is the message you want to publish.

Publishing a Message

To publish a message you need to:

  1. Create a client object.
  2. Create a client connection.
  3. publish the message
  4. Examine the return code of the publish request
  5. Examine the publish acknowledgement using the on_publish callback
Example code:
import paho.mqtt.client as paho
def on_publish(client,userdata,result):             #create function for callback
    print("data published \n")
client1= paho.Client("control1")                           #create client object
client1.on_publish = on_publish                          #assign function to callback
client1.connect(broker,port)                                 #establish connection
ret= client1.publish("house/bulb1","on")                   #publish

Note 1: you don’t appear to need a client loop to process the on_publish callback.

Note2: When you publish a message the publish method returns a tuple (result, mid).

The result is the error code. A result of 0 indicates success.

The mid value is an integer that corresponds to the published message number as assigned by the client.

The mid value can be used with the on_publish callback to check that the messages was published.

On_publish Callback

When the message has been published to the Broker an acknowledgement is sent that results in the on_publish callback being called.

The on_publish callback function must receive three parameters

on_publish(client, userdata, mid)

The client is the client object, and is useful when you have multiple clients that are publishing data.

The userdata is user defined data which isn’t normally used.

The mid value is the message id and can be used with the mid value returned from the publish method to check that a particular message has been published.

In the screen shot below I have printed the mid value from the publish message and on publish callback.

You can see that the numbers match which indicated that both messages were published OK


Message Flow and Quality of Service Example

If you want to try and ensure that the subscriber gets a message even though they might not be online then you need to publish with a quality of service of 1 or 2.

The schematic below shows the message flow between client and broker for messages with QOS of 0, 1 and 2.


The  screen shot below shows the results of running a test Python script that publishes 3 messages with QOS of 0,1 and 2 respectively


Notice with QOS set to 2 there are more messages generated.

In addition messages sent with QOS of 0 are not acknowledged by the broker but instead by the sending client.

There is no guarantee that the broker received them. See:

Publishing on Restricted Topics

You may not be allowed to publish messages on certain topics.

This is controlled by the broker See Configuring Topic Restriction on Mosquitto.

However what happens if you do publish on a restricted topic?

Well as far as the client is concerned the publish succeeds, and if I run the previous script but restrict the topic then my client output would be identical to that shown above.

However this is what I would see on the Sever (broker)


Notice the denied publish message on the server console. Also notice the normal publish acknowledgement sequence.

Retain Message Flag

When publishing a message you can also set the retain message flag.

This flag tells the broker to store the last message that you sent.

This is very useful if you only publish messages at infrequent intervals- See Retained messages-Example’

Disconnecting After Publish

If you only publish messages at infrequent intervals then you will probably want to disconnect the client between publishing.

You can use the disconnect method along with the on_disconnect callback for this.

This type of disconnect does not result in a last will and testament message.

def on_disconnect(client, userdata, rc):
   print("client disconnected ok")
client1.on_disconnect = on_disconnect

Publishing and The Loop

Although you can publish messages without starting a loop (See the Loop) you will need to start a loop or call the loop to send automatic ping messages.

Therefore you may find that your client gets disconnected , and without the loop you don’t see the disconnect message.

Therefore if you do this you will need to consider setting the keepalive to a higher value (60 seconds by default).

Note: If you publish messages inside the keep alive value then the connection isn’t closed by the server/broker.

Publishing Using SSL

You will need to add a broker/server CA key to the client, See Configuring Mosquitto for SSL for details about keys.

You then use the client.tls_set function before you connect. E.G.


Depending on how you’ve configured the broker you may need to use the TLS version parameter (code below set client to use TLSv1.2.


Because SSL normally uses port 8883 you will also need to change the port when connecting.

Publishing Using WebSockets

You can also publish messages over webSockets. You need to tell the client to use Websockets when creating the client object as show below:

Use the command

client= paho.Client(“control1”,transport=’websockets’)

instead of simply

client= paho.Client(“control1”)

The publish uses the same procedure and process as standard MQTT. See My MQTT WebSockets Notes

Publishing Video

Here is a video that I created that covers the main points from above. Grateful of any feedback

Common Questions and Answers

Q- Do I know if and when my published messages have been received by a client?

A- No. You only know that the broker has received them, and then only if the QOS is set to 1 or above.

Q- What happens if I publish on the wrong topic?

A- Generally unless the topic has an invalid topic address you wont know.

Q- What happens if I publish on a restricted topic?

A- You wont know as it will appear to succeed.

Q- How do I know that no one else is using that topic?

A- You don’t. You could subscribe to the topic beforehand and look for activity.

Q- Can I publish on any topic that I want to?

A- Yes unless the broker administrator has configured topic restrictions.

Course Links

  1. Introduction to the Paho Python MQTT Client
  2. Introduction to the Client Class
  3. Connecting to a Broker
  4. Publishing Using The Paho Python MQTT Client
  5. Subscribing using The Paho Python Client
  6. Understanding The Loop
  7. Understanding Callbacks
  8. Handling Multiple Client Connections.

Related Tutorials

MQTT Publish and Subscribe for Beginners

Please rate? And use Comments to let me know more
[Total: 16   Average: 4.7/5]


    1. You will need to put the publish in a while loop and send . I doubt if you will get 100 messages per second.
      Below is code I use on my tester. Adjust the delay to increase/decrease the message rate.

      while count<=loops: client.loop(0.001) msg_out="test message" client.publish(topic_out,msg_out) #print("message ",msg_out) time.sleep(delay) count+=1 except KeyboardInterrupt: print("interrrupted by keyboard") mcount=mcount-1 print("messages sent loop count ",count-1) time_taken=time.time()-stime print("time taken = ",time_taken) message_rate=mcount/time_taken print("message_rate ",message_rate) msg_out={"messages":mcount,"time taken":time_taken,"message_rate":message_rate}

  1. how to publish current date n time? this is my code. thx.

    # import paho mqtt
    import paho.mqtt.client as mqtt

    # import time untuk sleep()
    import time

    # import datetime untuk mendapatkan waktu dan tanggal
    import datetime

    # definisikan nama broker yang akan digunakan

    # buat client baru bernama P2
    print(“creating new instance”)
    client = mqtt.Client(“P2”)

    # koneksi ke broker
    print(“connecting to broker”)
    client.connect(broker_address, port=3333)

    # mulai loop client

    # lakukan 20x publish topik 1
    print(“publish something”)
    for i in range (20):
    # sleep 1 detik
    # publish waktu sekarang topik 1
    print(“Publishing message to topic”,”1″)

    #stop loop

  2. Dear Mr. Steve
    I appreciated your contribution. I try to create my own test system based on your tutorials.
    Step 1. Install mosquitto on window machine and execute some test cases. Everything is OK
    Step 2. Install paho in ubuntu machine. Write a client code by python to connect with Window machine.
    I start mosquitto_sub on window machine and try to publish a message from ubuntu.

    cmd terminal command
    mosquitto_sub -t ‘test/topic2’

    Code python in ubunutu in
    import paho.mqtt.client as paho
    def on_publish(client,userdata,result): #create function for callback
    print(“data published \n”)
    client1= paho.Client(“control1”) #create client object
    client1.on_publish = on_publish #assign function to callback
    client1.connect(broker,port) #establish connection
    ret= client1.publish(“test/topic2″,”hello from ubuntu”) #publish

    terminal command

    Nothing displayed on terminal on window.
    I already checked the network connection b/w 2 machines. It was fine. Is there any specific configuration in Mosquitto broker on window? Is possibility when the network firewall or something that terminated message? How can I check MQTT publish message flow from ubuntu machine.


  3. Hello, I have to use mqtt and for that i am going through your tutorial and i have a silly doubt like the code you have written in this tutorial for publishing messages, i used that code only and run it in jupyter notebook with a mosquitto broker, the code is running fine but i want to know:
    where can i see the messages which i have published ?
    can we have two way communication ?
    can i send a video file through this ?
    How can we store messages ?

      1. Thanks for reply.
        If other person wants to see the message which i have published how can he see the message he has to subscribe to the topic right but for that subscribe code has to run, my question is:
        subscribe code can be run seperately. If yes, then how, like you have told in one of your tutorial we have to subscribe before publishing, but other person need not to be publish only we publish the message and other persons just have to read it.

        sorry for the bad english.

        1. Yes the other person needs to subscribe. If he hasn’t subscribed and the message is published then he misses the message.
          MQTT works like TV. If your not watching TV when the 9pm news is on you miss it.

        2. “”””For Publisher””””
          import paho.mqtt.client as mqtt

          broker = “”
          port = 1883

          def on_publish(client, userdata, result):
          print(“data published \n”)

          client1 = mqtt.Client(“control1”)
          client1.on_publish = on_publish
          client1.connect(broker, port)
          sub = client1.subscribe(“simpletext/msg”)

          ret = client1.publish(“simpletext/msg “,”Hello good morning have a good day”)

          data published

          “”” For Subscriber”””

          import paho.mqtt.client as mqtt

          broker = “”
          port = 1883

          client1 = mqtt.Client(“control1”)
          client1.connect(broker, port)
          sub = client1.subscribe(“simpletext/msg”, 2)


          The thing is that for subscriber also message is not displayed it is giving an output in tuple form as you can see.
          Help me to clarify my doubts.

          Thank you

          1. Thanks Steve for helping me out and guiding me through my first mqtt program which did work.
            I will be here again if i need some help .

            Thanks a lot Steve.

            Best Regards

  4. Hello,

    I run on a rpi 3 with IO Pi Plus extender board and I want to run a python script that communicate with openhab2 through mqtt messages

    the code

    from IOPi import IOPi
    import time
    import paho.mqtt.client as mqtt
    #import json

    torec = 0

    def on_message(client, userdata, message):
    #print(“message received ” ,str(message.payload.decode(“utf-8”)))
    #print(“message topic=”,message.topic)
    #print(“message qos=”,message.qos)
    #print(“message retain flag=”,message.retain)
    if (message.retain == 0):
    #print(“mqtt received”,message.topic,”=”,str(message.payload.decode(“utf-8”)))
    torec = print(message.topic,str(message.payload.decode(“utf-8”)))

    def main():

    # Create client instance and connect to localhost
    client = mqtt.Client()

    bus1 = IOPi(0x20)
    bus2 = IOPi(0x21)

    bus2.set_pin_direction(1, 1) # set pin 1 as an input
    bus2.set_pin_pullup(1, 1) # enable the internal pull-up resistor on pin 1
    bus2.invert_pin(1, 1) # invert pin 1 so a button press will register as 1

    bus1.set_pin_direction(1, 0) # set pin 8 as an output
    bus1.write_pin(1, 0) # turn off pin 8

    while True:

    #print(“connecting to broker”)
    client.loop_start() #start the loop

    if (bus2.read_pin(1) == 1) and (bus1.read_pin(1) == 0) or (torec == “light/l01 ON”): # check to see if the button is pressed
    print (‘button 1 pressed turn on switch 1’) # print a message to the screen
    #value = bus2.read_interrupt_capture(0)
    bus1.write_pin(1, 1) # turn on pin 1
    client.publish(“light/l01”, “ON”)
    time.sleep(1) # sleep 1s before checking the pin again
    elif (bus2.read_pin(1) == 1) and (bus1.read_pin(1) == 1) or (torec == “light/l01 OFF”):
    print (‘button 1 pressed turn off switch 1’) # print a message to the screen
    bus1.write_pin(1, 0) # turn off pin 1
    client.publish(“light/l01”, “OFF”)
    time.sleep(1) # sleep 1s before checking the pin again

    #time.sleep(1) # wait
    client.loop_stop() #stop the loop

    if __name__ == “__main__”:

    I have a problem when I run it. It send mqtt messeges just fine but it only receives only whatever send it from the code. It doesnt reveive messeges from other source.

    1. The connect,subscribe,loop_start and stop should be outside the while loop. Correct that and try again.
      If you still have problems use the ask-steve page and send me the code.

      1. Thank you that was the problem. Now if you can help me about the variable. When I run the shell it always print and the 0 that was declared at the begging and after that print the topic at it should.

  5. I am trying to find an answer to the following questions:

    1. Is it possible to connect from one MQTT client to multiple MQTT brokers?
    In this case – does anything prevent me to use the same clientID for each connection?

    2. I am writing an application where I am planning to have multiple MQTT clients (Client1 and Client2)
    Is it possible to connect every client to a different MQTT broker?
    If yes, what happens if Broker1 received data on TopicX that Client1 subscribed and Broker2 receives data on TopicX that Client2 subscribed – will Client1 and Client2 receive the data on the same TopicX?

    Thank you in advance.

  6. Hi. How we convert the received message from string to integer. I want to use integer received message in if loop and compare with numbers. Im using python3 and rpi3 b+.


  7. Can we make broker to broker communication? If one broker was down, it tells to another broker that it was down. Sorry for bad english

    1. Currently no but I’m sure that will be developed as MQTT brokers become more established. However the client usually needs to detect a broker down so that it can switch to using another broker if one is configured

  8. Hello, I want to publish a message when I receive a certain message, my code is this:
    import time
    import paho.mqtt.client as mqtt
    from pylab import *
    from sunposition import sunpos
    from datetime import datetime

    def connect( client, userdata, flags, rc):
    print (“Conectado”)

    mystring = ‘send’
    b = bytes(mystring, ‘utf-8’)
    cabeceraok = 0

    def on_message( client, userdata, msg):
    if(msg.payload == b):

    client = mqtt.Client()
    client.on_connect = connect
    client.on_message = recepcion
    client.username_pw_set(mqttUser, mqttPassword)

    It receives the messages correctly but I cant get that client.publish to work. Can you give me some help?

  9. Messages published in 8883 port via TLS to a specific topic can be viewed in the 1883 port of mosquitto server in the same topic. Should that happen in a secure connection?

    1. Yes that is the way it works. The SSL connection is a link from the client to broker it is not client to client. You can send messages into the broker using standard MQTT and receive with another client using MQTT over SSL or Websockets etc.
      Does that make sense?

  10. I’ve been unable to find out about publishing various payload types, like ‘comma seperated variable files’.

    I’m appending 4 integer variables to a CSV text file to be used as a payload.

    Have you any experience of this? I’m going to make a PayPal contribution from my company for all the help you’ve given me.

  11. I’m using the paho mqtt broker in Raspbian Linux, and the ESP8266 Arduino PubSubClient.h library.
    I’m getting a -2 result from the rc connection return code (connection refused due to bad client id).
    I’ve read about clientid being restricted to alpha-numeric characters, no symbols like dashes etc, and that they have to be utf-8 encoded.

    I see from your example in the line:
    client1= paho.Client(“control1”)
    that you’ve used a regular ascii string to create the clientID.

    I’ve tried creating random clientID’s, but that requires a clean session to be set to true, and from the pubsubclient.h source code I don’t see the ability to set that anywhere. So best to avoid random ID’s.

    I don’t know if my arduino client library and paho broker are even compatible, but for now I’ll assume it is and the clientID string just needs to be understood.

    In your tutorial: “Paho Python MQTT Client Objects” it says clientID’s will be auto generated if the clientID parameter is left blank when clean session is set to true on instantiating a new client object. I don’t know if pubsubclient.h sets a clean session to true by default, or if it even uses that parameter.

    What do you know about guidelines/restrictions etc on naming of clientID’s that aren’t rejected by the broker?

    1. Alan

      This is taken from the specification

      The payload of the CONNECT Packet contains one or more length-prefixed fields, whose presence is 557 determined by the flags in the variable header. These fields, if present, MUST appear in the order Client 558 Identifier, Will Topic, Will Message, User Name, Password
      [MQTT-3.1.3-1]. 559 Client Identifier 560 The Client Identifier (ClientId) identifies the Client to the Server. Each Client connecting to the Server has 561 a unique ClientId. The ClientId MUST be used by Clients and by Servers to identify state that they hold 562 relating to this MQTT Session between the Client and the Server
      [MQTT-3.1.3-2]. 563
      564 The Client Identifier (ClientId) MUST be present and MUST be the first field in the CONNECT packet 565 payload [MQTT-3.1.3-3]. 566
      567 The ClientId MUST be a UTF-8 encoded string as defined in Section 1.5.3
      [MQTT-3.1.3-4]. 568 569 The Server MUST allow ClientIds which are between 1 and 23 UTF-8 encoded bytes in length, and that 570 contain only the characters 571 “0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”


Leave a Reply

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