Topic Alias - MQTT 5 Essentials Part 10
Written by Florian Raschbichler
Category: MQTT 5 MQTT 5 Essentials HiveMQ IoT
Published: January 3, 2020
In the MQTT 5 Essentials, we explore the foundational changes that MQTT v5 introduces and the top reasons MQTT users make the move to MQTT 5. Our goal is to individually examine each of the new features in the MQTT 5 protocol. Today, we’ll continue with a seemingly small feature that can have a potentially big impact on your business: Topic Alias.
For a full understanding of Topic Alias be sure to watch our video on this topic at the end of this blog post.
MQTT enables the use of standing connections between your devices and the brokers. The Keep Alive Mechanism ensures that connections between clients and broker are upheld as long as possible and that any connection loss, which may occur in unstable networks, is detected quickly. With PING packets in the size of two Bytes only having to be sent every few minutes, connections can be upheld at a low cost of power and bandwidth.
Today’s feature is specifically useful in deployments where large numbers of devices are connected and smaller messages are sent at a high frequency.
Topic Aliases are an integer value that can be used as a substitute for topic names. A sender can set the Topic Alias value in the PUBLISH message, following the topic name. The message receiver then processes the message like any other PUBLISH and persists a mapping between the Integer (Topic Alias) and String (Topic Name). Any following PUBLISH message for the same topic name can then be sent with an empty topic name, only using the defined Topic Alias.
Both the client and broker can define a Topic Alias for a PUBLISH message, as long as they are the respective sender. Similarly the client and the broker can restrict the number of Topic Aliases they allow per connection. The Topic Alias Maximum is communicated during connection establishment. The client can set it in the CONNECT and the broker in the CONNACK packet. Consequently the client must only use Topic Alias values between 1 and the Topic Alias Maximum the broker sent in the CONNACK. Likewise the broker is only permitted to use values between 1 and the maximum sent in the CONNECT by the client. If no Topic Alias Maximum value is set, a value of 0 is assumed and the use of Topic Aliases is not permitted.
MQTT is a lightweight communication protocol. Standing TCP connections between client and broker can be upheld with little energy and bandwidth consumption via the Keep Alive mechanism. This enables MQTT users to build deployments with permanently connected devices at low cost.
Minimal data points like measurements can be delivered in real time. There is no need to collect data and send data in bulk packages periodically anymore, as it is necessary with other less lightweight technologies for data transfer.
In a number of use cases, such as predictive maintenance, the quality and responsiveness of the service can be improved by sending small data points in real time. Implementing a use case like this, the deep and individualized topic name may exceed the size of the actual payload, that may be represented by a single integer value. For example a topic name like data/europe/germany/south/bavaria/munich/schwabing/box-32543y/junction/consumption/current describes the current power consumption of a specific junction box. The value to be sent is a single integer.
So when you want to send many small messages in real time with long topic names, the Topic Alias feature really shines and helps to increase performance. More importantly it reduces network traffic significantly.
Summary and Gotchas
- Topic Aliases substitute UTF-8 String topic names with an integer
- Mapping of Topic Alias to Topic is always only relevant for a single connection
- Supporting this feature is optional for brokers and clients
- Broker and client negotiate to what degree this feature is supported during the connection establishment
- Be sure to use a broker and client implementation that supports Topic Aliases, if you want to introduce this feature
- Used correctly, Topic Aliases can have significant impact on the profit margins of your business case
Let us know about a use case that you think Topic Aliases can be very useful for in the comments!
Learn everything about MQTT v5
Over the next few weeks, we will continue our technical deep dive into all of the MQTT 5 features. In Part 11 of this series we will be talking about Enhanced Authentication. If you would like to join us, sign up for our newsletter to get regular updates and ensure that you don’t miss a post.