Pub-Sub MQTTv5 Using The Paho Python Client

Previous Tutorial Connecting to a Broker Using MQTTv5 and python.

Again this tutorial assumes you are already familiar with the basics after using MQTTv3.1.1.

This is covered in these tutorials:

Publishing

Just as with MQttv3.1.1 we have a publish function and also the on_publish callback.

Just as with the connection we have an additional properties object to deal with.

Old signature for MQTT v3.1.1 and v3.1 is:
publish(self, topic, payload=None, qos=0, retain=False)
and for the MQTT v5.0 client:
publish(self, topic, payload=None, qos=0, retain=False, properties=None)

To use properties in the publish message we need to get a publish properties object as shown in the code below:

properties=Properties(PacketTypes.PUBLISH)

The following properties are available (links are to specification)

The payload format indicator lets you indicate to the receiver the type of data being sent and is a one byte field.

There is really currently only 1 option which is UFT-8 data. Indicator set to 1.

Setting it to 0 or not setting it means that the receiver must know the format in advance or guess as with MQTTv3.1.1.

The message expiry interval is a 4 byte field which tells the server to discard the message if hasn’t been delivered in this time period.

You should not that publish properties are sent by the broker to all subscribing clients which we will see when examining a received message.

We will construct our publish properties object using these two fields as examples.

print("publishing ")
properties=Properties(PacketTypes.PUBLISH)
properties.PayloadFormatIndicator=1
properties.MessageExpiryInterval=15 #expire message after 15seconds

The publish message looks like this:

client_pub.publish(pub_topic, msg_out,qos=1,properties=properties)

Note: We need to publish with a QOS of 1 or 2 to test the message expiry interval.

Note: Publish returns 0 on success, but you need to process the on_publish callback to be sure.

on_publish Callback

There is currently no change in this callback function from v3.1.1. Even though the broker will return a reason code 135 when a publish is denied due to ACL restrictions the client cannot detect it.

There is no properties parameter in the callback.

Subscribing

In order to test the message expiry interval,and session expiry interval we to subscribe using a QOS of 1 or 2 .

In addition we need to set a session expiry interval and also connect with a clean_start flag of False.

The sample code is shown below:

properties=Properties(PacketTypes.CONNECT)
print("Setting session expiry interval")
properties.SessionExpiryInterval=300 #set session expiry interval=5 mins
client_sub.connect(host,port,clean_start=False,properties=properties)
client_sub.subscribe(pub_topic,qos=1)

Examining the Received Message

When we published the message we set the message type as UTF-8.

We can check this isĀ  the received message by looking at the received properties in the message object in the on_message callback.

def on_message(client, userdata, message):

msg=str(message.payload.decode("utf-8"))
messages.append(msg)
print("message properties",message.properties)
print('RECV Topic = ',message.topic)
print('RECV MSG =', msg)

A screen shot of the result is shown below:message-properties-mqtt5

Note: Subscribe returns 0 on success, but you need to process the on_subscribe callback to be sure. In addition ACL restrictions are not detected by the client.

on_subscribe Callback

Again we have a properties object to consider.

Old signature for MQTT v3.1.1 and v3.1 is:
on_ subscribe(client, userdata, mid, granted_qos)

and for the MQTT v5.0 client:
on_subscribe(client, userdata, mid, granted_qos,rc,properties=None)

Note: The on line documentation doesn’t include the reason codes (rc) and the callback does work without it. However the actual code does include it.

Demo Code- pub_sub.py in the zip file.

download

Related Tutorials and Resources:

Please rate? And use Comments to let me know more

Leave a Reply

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