小程序
传感搜
传感圈

An Introduction to Subscription Options in MQTT

2023-08-13 01:58:39
关注

Illustration: © IoT For All

In this article, we will focus on exploring the available subscription options in MQTT and their usage.

A subscription in MQTT consists of a topic filter and corresponding subscription options. So, we can set different subscription options for each subscription.

MQTT 5.0 introduces four subscription options: QoS, No Local, Retain As Published, and Retain Handling. On the other hand, MQTT 3.1.1 only provides the QoS subscription option. However, the default behavior of these new subscription options in MQTT 5.0 remains consistent with MQTT 3.1.1. This makes it user-friendly if you plan to upgrade from MQTT 3.1.1 to MQTT 5.0.

Subscription Options

Now, let’s explore the functions of these subscription options together.

QoS

QoS is the most commonly used subscription option, which represents the maximum QoS level that the server can use when sending messages to the subscriber.

A client may specify a QoS level below 2 during subscription if its implementation does not support QoS 1 or 2.

Additionally, if the server’s maximum supported QoS level is lower than the QoS level requested by the client during the subscription, it becomes apparent that the server cannot meet the client’s requirements. In such cases, the server informs the subscriber of the granted maximum QoS level through the subscription response packet (SUBACK). The subscriber can then assess whether to accept the granted QoS level and continue communication.

EMQ Technologies Inc.

A simple calculation formula:

The maximum QoS granted by the server = min ( The maximum QoS supported by the server, The maximum QoS requested by the client )

However, the maximum QoS level requested during subscription does not restrict the QoS level used by the publishing end when sending messages. When the requested maximum QoS level during subscription is lower than the QoS level used for message publishing, the server will not ignore these messages. To maximize message delivery, it will downgrade the QoS level of these messages before forwarding.

EMQ Technologies Inc.

Similarly, we have a simple calculation formula:

QoS in the forwarded message = min ( The original QoS of the message, The maximum QoS granted by the server )

No Local

The No Local option has only two possible values, 0 and 1. A value of 1 indicates that the server must not forward the message to the client that published it, while 0 means the opposite.

This option is commonly used in bridging scenarios. Bridging is essentially two MQTT Servers establishing an MQTT connection, then they subscribe to some topics from each other. The server forwards client messages to another server, which can continue forwarding them to its clients.

EMQ Technologies Inc.

Let’s consider the most straightforward example where we assume two MQTT servers, Server A and Server B.

They have subscribed to the topic # from each other. Now, Server A forwards some messages from the client to Server B, and when Server B looks for a matching subscription, Server A is there too. If Server B forwards the messages to Server A, then Server A will forward them to Server B again after receiving them, thus falling into an endless forwarding storm.

However, if both Server A and Server B set the No Local option to 1 while subscribing to the topic #, this problem can be ideally avoided.

Retain As Published

The Retain As Published option also has two possible values, 0 and 1. Setting it to 1 means the server should preserve the Retain flag unchanged when forwarding application messages to subscribers and setting it to 0 means that the Retain flag must be cleared.

Like the No Local option, Retain As Published primarily applies in bridge scenarios.

We know that when the server receives a retained message, in addition to storing it, it will also forward it to existing subscribers like a normal message, and the Retain flag of the message will be cleared when forwarding.

This presents a challenge in bridge scenarios. Continuing with the previous setup, when Server A forwards a retained message to Server B, the Retain flag is cleared, causing Server B not to recognize it as a retained message and not store it. This makes retained messages unusable across bridges.

In MQTT 5.0, we can let the bridged server set the “Retain” publish option to 1 when subscribing to solve this problem.

EMQ Technologies Inc.

Retain Handling

The Retain Handling subscription option indicates to the server whether to send retained messages when a subscription is established.

When a subscription is established, the retained messages matching the subscription in the server will be delivered by default.

However, there are cases where a client may not want to receive retained messages. For example, if a client reuses a session during connection but cannot confirm whether the previous connection successfully created the subscription, it may attempt to subscribe again. If the subscription already exists, the retained messages may have been consumed, or the server may have cached some messages that arrived during the client’s offline period. In such cases, the client may not want to receive the retained messages the server publishes.

Additionally, the client may not want to receive the retained message at any time, even during the initial subscription. For example, we send the state of the switch as a retained message, but for a particular subscriber, the switch event will trigger some operations, so it is helpful not to send the retained message in this case.

We can choose among these three different behaviors using Retain Handling:

  • Setting Retain Handling to 0 means that retained messages are sent whenever a subscription is established.
  • Setting Retain Handling to 1 means retained messages are sent only when establishing a new subscription, not a repeated one.
  • Setting Retain Handling to 2 means no retained messages are sent when a subscription is established.

Tweet

Share

Share

Email

  • MQTT
  • Network and Protocols

  • MQTT
  • Network and Protocols

参考译文
MQTT 订阅选项简介# 示例输入与输出**输入** 人工智能(AI)是计算机科学的一个分支,旨在开发表现出人类智能的软件或机器。这包括从经验中学习、理解自然语言、解决问题以及识别模式。**输出** 人工智能(AI)是计算机科学的一个分支,旨在开发表现出人类智能的软件或机器。这包括从经验中学习、理解自然语言、解决问题以及识别模式。
插图:© IoT For All --> 在本文中,我们将专注于探讨MQTT中可用的订阅选项及其用法。MQTT中的订阅由一个主题过滤器和相应的订阅选项组成。因此,我们可以为每个订阅设置不同的订阅选项。MQTT 5.0引入了四个订阅选项:QoS、No Local、Retain As Published和Retain Handling。而另一方面,MQTT 3.1.1仅提供QoS订阅选项。然而,MQTT 5.0中这些新订阅选项的默认行为与MQTT 3.1.1保持一致。这使得如果您计划从MQTT 3.1.1升级到MQTT 5.0,使用起来将更加友好。订阅选项现在,让我们一起来探讨这些订阅选项的功能。QoSQoS是最常用的订阅选项,它表示服务器在向订阅者发送消息时可以使用的最高QoS级别。如果客户端的实现不支持QoS 1或QoS 2,它可以在订阅时指定低于2的QoS级别。此外,如果服务器支持的最高QoS级别低于客户端在订阅时请求的QoS级别,很明显服务器无法满足客户端的需求。在这种情况下,服务器会通过订阅响应包(SUBACK)告知订阅者被授予的最高QoS级别。订阅者可以据此判断是否接受被授予的QoS级别并继续通信。EMQ Technologies Inc.一个简单的计算公式:服务器授予的最高QoS = 最小(服务器支持的最高QoS,客户端请求的最高QoS)然而,订阅过程中请求的最高QoS级别不会限制发布端在发送消息时使用的QoS级别。当订阅请求的最高QoS级别低于消息发布使用的QoS级别时,服务器并不会忽略这些消息。为最大程度地实现消息传递,它会在转发之前将这些消息的QoS级别降级。EMQ Technologies Inc.同样,我们有一个简单的计算公式:转发消息的QoS = 最小(消息的原始QoS,服务器授予的最高QoS)No LocalNo Local选项只有两个可能的值,0和1。值为1表示服务器必须不能将消息转发给发布该消息的客户端,而值为0则表示相反。这个选项通常用于桥接场景。桥接本质上就是两个MQTT服务器建立MQTT连接,然后它们相互订阅一些主题。服务器将客户端的消息转发给另一个服务器,后者可以继续将消息转发给其客户端。EMQ Technologies Inc.让我们考虑一个最简单的例子,假设两个MQTT服务器,服务器A和服务器B。它们相互订阅了主题#。现在,服务器A将一些消息从客户端转发给服务器B,而当服务器B查找匹配的订阅时,服务器A也在那里。如果服务器B将消息转发给服务器A,那么服务器A接收到这些消息后,又会将它们转发给服务器B,从而导致一个无尽的转发风暴。然而,如果服务器A和服务器B在订阅主题#时都将No Local选项设置为1,就可以理想地避免这个问题。Retain As PublishedRetain As Published选项也有两个可能的值,0和1。将其设置为1表示服务器在将应用程序消息转发给订阅者时应保留Retain标志不变,而将其设置为0则表示必须清除Retain标志。与No Local选项类似,Retain As Published主要应用于桥接场景。我们知道当服务器接收到保留消息时,除了存储它之外,还会像普通消息一样将它转发给现有订阅者,而转发时该消息的Retain标志会被清除。这在桥接场景中带来了问题。继续沿用之前的设置,当服务器A将一个保留消息转发给服务器B时,Retain标志被清除,导致服务器B无法识别它是一个保留消息,也不会存储它。这使得保留消息在桥接中无法使用。在MQTT 5.0中,我们可以在订阅时让桥接服务器将“Retain”发布选项设置为1,以解决这个问题。EMQ Technologies Inc.Retain HandlingRetain Handling订阅选项指示服务器在建立订阅时是否发送保留消息。当建立订阅时,服务器中匹配订阅的保留消息默认会进行发送。然而,有些情况下客户端可能不希望接收保留消息。例如,如果客户端在连接时重用了一个会话,但无法确认上一次连接是否成功建立了订阅,它可能会尝试再次订阅。如果订阅已经存在,保留消息可能已经被消费,或者服务器可能缓存了客户端离线期间到达的一些消息。在这种情况下,客户端可能不希望接收到服务器发布的保留消息。此外,客户端可能在任何时候都不希望接收保留消息,即使在初次订阅时。例如,我们将开关的状态作为保留消息发送,但对某个特定订阅者来说,开关事件会触发一些操作,因此在这种情况下,不发送保留消息是有帮助的。我们可以根据以下三种不同的行为选择Retain Handling:将Retain Handling设置为0表示每次建立订阅时都会发送保留消息。将Retain Handling设置为1表示只有在建立新订阅时才发送保留消息,而不是在重复订阅时。将Retain Handling设置为2表示在建立订阅时不会发送任何保留消息。TweetShareShareEmail MQTTNetwork and Protocols --> MQTTNetwork and Protocols
您觉得本篇内容如何
评分

评论

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

提交评论

广告

iotforall

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

关注

点击进入下一篇

刑事打击“吃鸡”游戏外挂,哪个罪名更适宜?

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