Receiving Messages with the Paho MQTT Python Client

Messages are received by the on_message callback, and so this callback must be defined and bound in the main script.

All callbacks rely on the client loop and this must also have been started using loop_start() or loop_forever(), or run manually within the main script. See Understanding the loop for more details

Provided this is the case it is easy to receive the messages and display them on the console using the example code below:

def on_message(client, userdata, message):
    print("received message =",str(message.payload.decode("utf-8")))

Message is an object and the payload property contains the message data which is binary data.

To access the message from you main script requires a little more work as the on_message callback is asynchronous and also usually run in a separate thread.

There are three common methods of doing this:

  1. Use a global variable
  2. Use a list
  3. Use the queue object

Using a Global Variable

You could use an on_message callback as show below:

def on_message(client, userdata, message):
global message_received

The message received variable is defined in the main script and can also be used there.

However how do you test that you have a message and what happens if you get another message before you can process the first message?

Use a List

We can declare a list in the main loop and use it in the callback . It doesn’t need to be a global variable.

The callback would look like this:

def on_message(client, userdata, message):

received_messages.append(message)#use an array

and in the main script we would have code like this

print("received in array",str(message.payload.decode("utf-8")))

However this will potentially cause problems when the on_message callback is in another thread as they use a shared array.

Use the Queue object

This is by far the best option as the queue object is thread safe.

To use the queue object you must import it.

We use it very much like an array as shown in the code.

In the main loop we create a queue and extract messages from it using:

from queue import Queue

#later in code
while not q.empty():
   message = q.get()
   if message is None:
   print("received from queue",str(message.payload.decode("utf-8")))

The on_message callback would look like this:

def on_message(client, userdata, message):

If you want to learn more about the queue object I recommend this pymow tutorial

This is the technique I use in my data logger scripts listed in the resources below.

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.  Receiving Messages with the Paho MQTT Python Client
  7. Understanding The Loop
  8. Understanding Callbacks
  9. Handling Multiple Client Connections
Please rate? And use Comments to let me know more

Leave a Reply

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