MQTT v5 provides many new features compared to MQTT v3.1 and v3.1.1. We will try to present these features in an accessible way and discuss their impact on developers. We've already discussed some of the MQTT v5 new features , and we'll continue the discussion today: topic alias .
What is a topic alias
Topic Alias is a new feature related to topic names added in MQTT v5.0. It allows users to reduce the bandwidth consumption when publishing messages by reducing the long and commonly used topic name to a double-byte integer.
It is a two-byte integer and will be encoded as an attribute field in the variable header part of the PUBLISH
And in practical applications, it will be limited by the CONNECT
"Maximum Length of Subject Aliases" in the CONNACK
and 061ea5a0104c94 packets. As long as this limit is not exceeded, any topic name can be reduced to an integer with an encoded length of 2 bytes using this feature.
Why use topic aliases
When using the MQTT v3 protocol. If the client needs to publish a large number of messages with the same topic in a certain connection, then PUBLISH
message will cause a waste of bandwidth resources between the client and the server. At the same time, for the server, parsing the UTF-8 string of the same topic name every time is a waste of computing resources.
Consider this scenario, a sensor reporting temperature and humidity at a fixed frequency A
- Use
/position/A/temperature
as the subject of the temperature message at this location (length 23 bytes); - Use
/position/A/humidity
as the subject of the humidity message at this location (20 bytes in length).
Except for the first release of the message, each PUBLISH
message needs to transmit the already-delivered information of "topic name" through the network again. Even aside from the extra bandwidth consumption between the client and the server, for the server, in the face of a large number of messages published by thousands of sensors, for each message from each client, the same The topic name string is parsed, which will cause a waste of computing resources.
At this time, using the topic alias feature in MQTT v5 can effectively reduce resource consumption. When the client or server publishes frequently and the topic name length is large, using topic aliases can reduce the bandwidth consumption of topic names in each message to 2 bytes, and because the efficiency of computer processing integers is higher than The efficiency of processing strings also saves the computing resources consumed by the client or server during packet parsing.
How to use topic aliases
Topic alias life cycle and scope
This value by the client and server are maintained , and the life cycle and the scope is limited to the current connection. After the connection is disconnected, the topic alias needs to be used again, and the topic alias <=> topic name mapping relationship needs to be re-established.
Topic Alias Maximum
Before the MQTT client and the server use the topic alias to publish messages, it is necessary to agree on the maximum length of the topic alias that can be used. This part of the information exchange will be completed in the CONNECT
message and the CONNACK
message. The "Topic Alias Max" will also be encoded in the variable header of the CONNECT
and CONNACK
"Maximum topic aliases" in the CONNECT
message of the client indicates the maximum number of topic aliases that the server can use in this connection; similarly, in the CONNACK
message sent by the server, this value also indicates The maximum number of topic aliases that the peer (client) can use in the current connection.
Setting up and using topic aliases
When the client (or server) sends a PUBLISH
message, it can use a one-byte 0x23
in the attribute part of the variable header to indicate that the next 2 bytes will be the topic alias value.
However, the topic alias value is not allowed to be 0, nor is it allowed to be greater than the maximum topic alias value set in CONNACK
( CONNECT
End receives with the topic alias value and non-empty subject name PUBLISH
message, it will establish a mapping between the alias topic and theme name, after sending the PUBLISH
message, you can only 2 bytes in length To publish a message with a topic alias, the peer will use the previously established topic alias <=> topic name mapping relationship to process the topic in the message. And because this mapping relationship is maintained by both ends, the client and the server can publish messages to each other using topic aliases with the same value.
Use unset topic alias
PUBLISH
message is not set in the previous message, that is, the peer has not established the mapping relationship between the current topic alias and a topic name, and the topic name in the variable header of this message If the segment is empty, the peer will use the DISCONNECT
0x82
to disconnect the network.
reset topic alias
When the peer has created a topic alias <=> topic name
PUBLISH
packet in this connection, it can use the same topic alias value and non-empty topic name when PUBLISH
to update the mapping between the topic alias value and the topic name.
Summarize
As a new feature of MQTT v5, topic aliases pub/sub . For messages with consistent topic names, a large number and high repetition, it can effectively save bandwidth resources. and computing resources.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。