MQTT订阅非干净连接是幂等的吗

Is MQTT subcribe idempotent for non clean connection

我有一个订阅了多个主题的 MQTT 客户端。我设置了 clean session falseQoS 1 所以当我离线时消息将被排队。

但每次我上线时,我都会在 init 函数中使用相同的标志订阅相同的主题。会不会有什么问题?

each time I come online, I subscribe same topics with same flags in init function. Would it cause any problems?

MQTT (3.1.1) spec 声明:

If a Server receives a SUBSCRIBE Packet containing a Topic Filter that is identical to an existing Subscription’s Topic Filter then it MUST completely replace that existing Subscription with a new Subscription. The Topic Filter in the new Subscription will be identical to that in the previous Subscription, although its maximum QoS value could be different. Any existing retained messages matching the Topic Filter MUST be re-sent, but the flow of publications MUST NOT be interrupted [MQTT-3.8.4-3].

MQTT V5的情况类似:

If a Server receives a SUBSCRIBE packet containing a Topic Filter that is identical to a Non‑shared Subscription’s Topic Filter for the current Session, then it MUST replace that existing Subscription with a new Subscription [MQTT-3.8.4-3]. The Topic Filter in the new Subscription will be identical to that in the previous Subscription, although its Subscription Options could be different. If the Retain Handling option is 0, any existing retained messages matching the Topic Filter MUST be re-sent, but Applicaton Messages MUST NOT be lost due to replacing the Subscription [MQTT-3.8.4-4].

因此订阅客户端已经订阅的主题(具有相同的 QOS 等)应该不会导致任何问题(您可能会收到保留的消息)并且是一种常见的模式(例如 and this)。