Examining MQTTv5 User Properties

user-propertiesProperties (MQTTv5) are probably one of the most important additions to the MQTT protocol, and are available in most MQTT message types.

The property fields are dependent upon message type, so a CONNECT message will have different property fields than a PUBLISH message.



The User Property field is part of the properties field and consists of a series of UTF-8 key/value pairs.

If you are familiar with http it looks very similar to how http headers work.

User Properties effectively allow the user to extend the MQTT protocol and can be present in all messages and responses.

Because user properties are user defined they only have meaning to that user implementation.

User properties can be sent between client and server and client to client depending on the message type used to send them.

User properties set in the connection message for example aren’t passed on to the receiving client, whereas user properties set in the publish message are sent to the receiving client.

Connection User Properties

Connection message user defined properties are sent from client to server, and they are not transmitted on to the receiving client.

They are used to send connection related information to the server.

The server must be able to use these properties, and so these properties would be defined by the server, and so are server dependent.

Publish User Properties

These are probably the most useful as they are transmitted to the receiving client.

They will be most probably be used for sending meta data between clients. e.g

  • Message numbering
  • Timestamp
  • Routing infomation

To illustrate I have a simple publish and subscribe script that use a client to publish and another client to receive the message.

The publisher set user properties using the code below

print("sending message user properties set")
client_pub.publish("test/mqtt","test message",\
user_property=[('sfilename', 'test.txt'),('dfilename', 'test.txt')])

It could be used for example for file transfer where the file names are sent in the user properties and the file contents in the payload.

The screen shot below shows what is received.

publish-user-properties

Summary

The addition of properties field was probably the most significant change in the MQTT protocol, and allowed much more control information to be exchanged, and importantly user defined control information using user properties.

Resources:

Related Tutorials:

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

Leave a Reply

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