MQTTv5 Topics and Topic Aliases

MQTTv5 Topics are essentially the same as those in v3.1.1 (See Understanding MQTT topics) ,but two new features have been introduced. They are:

  • Topic aliases
  • Shared Topics

In this tutorial we will be covering topic aliases.



Topic aliases

Topic aliases were introduced in MQTT-SN and are mechanism for reducing the size of published packets by reducing the size of the topic field.

Once established a topic alias can be used in place of a topic string.

So if the topic houses/house1/upstairs/light1 had an alias or 1 then this could be used in place of the topic string when publishing messages.

Topic Aliases Client to Server

How many topic aliases the client can publish on is determined by the server. Mosquitto is set to 10 by default.

Topic Aliases Server to Client

How many topic aliases the server can publish on is determined by the client. Most clients use 0 by default. Which means that the server isn’t allowed to use topic aliases when publishing messages to the client.

Topic Alias Process

The topic alias for a topic is set in the publish message.

So for a client the client will publish a message on a topic and indicate that the topic will use an alias in the future.

Here is an example publish using Python:

client_pub.publish('test', msg_out1,qos=0,topic_alias=1)

Topic Alias Restrictions

Both the server and the client can impose restrictions on the number of topic aliases it will support on a connection.

For mosquitto the default is 10 and for the client it is 0.(no topic aliases supported)

You can see the number of aliases the server support in the CONNACK message.

topic-aliases-restrictions

The client indicates the client maximum this in the connection properties field.

In either case a value of 0 or not included means the client/server doesn’t support topic aliases.

If a client tries to publish using a topic alias and the number of topic aliases has been exceeded then the connection is dropped by the server.

The screenshot below shows the server has a maximum topic alias of 0 so when I try to publish the client gets disconnected.

Topic-Alias-Restrictions

Publishing and Subscribing Using Topic Aliases

Topic aliases are only available on publish and not on subscribe.

So if we have a topic called mytopicalias then the subscriber must subscribe using the full topic name whereas the publisher can assign an alias to the topic name and publish using the alias.

So the process for the publisher is:

publish message on topic and set the topic alias

Python example code:

client_pub.publish('mytopicalias', msg_out1,qos=0,topic_alias=1)

Publish subsequent messages using a blank topic and an alias

Python example code:

client_pub.publish('', msg_out1,qos=0,topic_alias=1)

The process for the subscriber is as a normal subscribe using full topic name.
Python example code:

client_sub.subscribe('mytopicalias',qos=0)

Related Tutorials and Resources

 

 

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 *