MQTT Subscription Identifier Explained

2023-08-07 22:56:58
关注

Illustration: © IoT For All

Most implementations of MQTT clients use a callback mechanism to handle incoming messages. Within the callback function, we only have access to the topic name of the message. If it is a non-wildcard subscription, the topic filter used during the subscription will be identical to the topic name in the message.

Therefore, we can directly establish a mapping between the subscribed topics and callback functions. Then, upon message arrival, we can look up the corresponding callback based on the topic name in the message and execute it.

However, if it’s a wildcard subscription, the topic name in the message will be different from the original topic filter used during the subscription. In this case, we need to match the topic name in the message with the original subscription one by one to determine which callback function should be executed. This affects the processing efficiency of the client.

EMQ Technologies Inc.

In addition, MQTT allows a client to establish multiple subscriptions, so a single message can match multiple client subscriptions when using the wildcard subscription.

In such cases, MQTT allows the server to send a separate message for each overlapping subscription or only one message for all the overlapping subscriptions. The former option means that the client will receive multiple duplicate messages.

Regardless of whether it’s the former or latter option, the client cannot determine which subscription(s) the message originated from. For example, even if the client finds that a message matches two of its subscriptions, it cannot guarantee that both subscriptions have been successfully created when the server forwards the message to itself. Therefore, the client cannot trigger the correct callback for the message.

EMQ Technologies Inc.

How Does the Subscription Identifier Work?

To address this issue, MQTT 5.0 introduced Subscription Identifiers. Its usage is very simple: clients can specify a Subscription Identifier when subscribing, and the server needs to store the mapping relationship between the subscription and the Subscription Identifier. When a PUBLISH packet matches a subscription and needs to be forwarded to the client, the server will return the subscription identifier associated with the subscription to the client together with the PUBLISH packet.

EMQ Technologies Inc.

If the server chooses to send separate messages for overlapping subscriptions, each PUBLISH packet should include the Subscription Identifier that matches the subscription. If the server chooses to send only one message for overlapping subscriptions, the PUBLISH packet will contain multiple Subscription Identifiers.

The client only needs to establish a mapping between Subscription Identifiers and callback functions. By using the Subscription Identifier in the message, the client can determine which subscription the message originated from and which callback function should be executed.

EMQ Technologies Inc.

In the client, the Subscription Identifier is not part of the session state, and its association with any content is entirely determined by the client. Therefore, besides callback functions, we can also establish mappings between Subscription Identifiers and subscribed topics, or between Subscription Identifiers and the Client ID. The latter is particularly useful in gateway scenarios where the gateway receives messages from the server and needs to forward them to the appropriate client. With the Subscription Identifier, the gateway can quickly determine which client should receive the message without re-matching and routing the topics.

A SUBSCRIBE packet can only contain one Subscription Identifier. If a SUBSCRIBE packet includes multiple subscriptions, the same Subscription Identifier will be associated with all those subscriptions. So, please ensure that associating multiple subscriptions with the same callback function is intentional.

Tweet

Share

Share

Email

  • MQTT
  • Network and Protocols

  • MQTT
  • Network and Protocols

参考译文
MQTT 订阅标识符解析
图解:© IoT For All 大多数MQTT客户端的实现都会使用回调机制来处理接收到的消息。在回调函数中,我们只能访问消息的主题名称。如果这是一个非通配符订阅,订阅时使用的主题过滤器将与消息中的主题名称完全一致。因此,我们可以直接建立订阅主题与回调函数之间的映射关系。当消息到达时,我们可以通过消息中的主题名称查找对应的回调函数并执行它。然而,如果是通配符订阅,消息中的主题名称将与订阅时使用的原始主题过滤器不同。在这种情况下,我们需要将消息中的主题名称逐一与原始订阅进行匹配,以确定应执行哪个回调函数。这将影响客户端的处理效率。此外,MQTT允许客户端建立多个订阅,因此当使用通配符订阅时,单条消息可以同时匹配多个客户端订阅。在这种情况下,MQTT允许服务器为每个重叠的订阅单独发送一条消息,或者为所有重叠的订阅只发送一条消息。前者意味着客户端将收到多个重复的消息。无论采用前者还是后者,客户端都无法确定该消息来自哪个订阅。例如,即使客户端发现某条消息匹配了两个订阅,它也无法保证当服务器将消息转发给自身时这两个订阅都已成功创建。因此,客户端无法触发正确的回调函数来处理该消息。EMQ Technologies Inc. 订阅标识符是如何工作的?为了解决这个问题,MQTT 5.0引入了订阅标识符(Subscription Identifier)。其用法非常简单:客户端在订阅时可以指定一个订阅标识符,服务器需要存储订阅与订阅标识符之间的映射关系。当PUBLISH数据包匹配某个订阅且需要转发给客户端时,服务器会将与该订阅相关的订阅标识符连同PUBLISH数据包一起返回给客户端。EMQ Technologies Inc. 如果服务器选择为重叠的订阅发送单独的消息,每个PUBLISH数据包都应包含匹配该订阅的订阅标识符。如果服务器选择为重叠的订阅只发送一条消息,该PUBLISH数据包将包含多个订阅标识符。客户端只需在订阅标识符与回调函数之间建立映射。通过使用消息中的订阅标识符,客户端可以确定消息的来源订阅以及应执行的回调函数。EMQ Technologies Inc. 在客户端中,订阅标识符不是会话状态的一部分,它与任何内容的关联完全由客户端决定。因此,除了回调函数之外,我们还可以在订阅标识符与订阅主题之间,或在订阅标识符与客户端ID之间建立映射关系。后者在网关场景中尤为有用,当网关接收到服务器发来的消息并需要将其转发给正确的客户端时,网关可以通过订阅标识符快速确定消息的接收客户端,而无需重新匹配和路由主题。一个SUBSCRIBE数据包只能包含一个订阅标识符。如果一个SUBSCRIBE数据包包含多个订阅,相同的订阅标识符将与所有这些订阅相关联。因此,请确保将多个订阅关联到同一个回调函数是出于有意为之的目的。TweetShareShareEmail MQTT网络与协议 MQTT网络与协议
您觉得本篇内容如何
评分

评论

您需要登录才可以回复|注册

提交评论

广告
提取码
复制提取码
点击跳转至百度网盘