小程序
传感搜
传感圈

MQTT Subscription Identifier Explained

2023-08-07
关注

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
您觉得本篇内容如何
评分

相关产品

物通博联网络科技 Modbus转MQTT网关WG583 数据采集物联网 物联网终端

物通博联网络科技 Modbus转MQTT网关WG583 数据采集物联网 物联网终端

深圳恒星物联 传感节点接入网关 传感节点网关

传感节点接入网关是一款工业级网关,可将Modbus RTU协议的设备数据转换为MQTT协议格式再远传至数据中心,支持采集数字量输入信号,支持输出数字量控制信号。

嘉兴博传 边缘计算网关系列产品 边缘计算网关

数据可以通过MQTT协议传输到用户的云彩或服务器上。该网关可以通过web浏览器访问,因此不需要安装软件,并且可以立即与传感器一起使用。

斯塔克电子 JK-5.0-S1安卓系统云组态屏(数据采集器\RTU遥测终端) 智能硬件(数据采集器、传感器)

同时支持MQTT或modbus,国标212协议等对接第三方软件平台,实现远程人机交互、远程下载更新程序等。

Tengrui 腾锐视讯 R-NB60-A 数据采集仪

以嵌入式实时操作系统为软件支撑平台,提供RS232、RS485、CAN接口,可直接连接串口设备,实现数据透明传输功能;低功耗设计;符合中国移动、联通、电信等蜂窝物联网通用模组技术规范,支持LWM2M、MQTT

评论

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

提交评论

iotforall

这家伙很懒,什么描述也没留下

关注

点击进入下一篇

基于STM32设计的智能家居控制系统(华为云IOT)

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