MQTT 共享订阅

MQTT shared subscription

使用 MQTT 共享订阅,有关订阅主题的消息只会发送给 一个 订阅客户端。那么,同组的其他客户端也订阅了同一个主题,如何接收消息呢?

With a MQTT shared subscription, the message on the subscribed topic would only be sent to one of the subscribing clients.

正确。对于正常的(non‑shared) subscription the messages are sent to ALL subscribers; with shared 订阅,它们将发送给一个订阅者。

在引入共享订阅之前,很难处理需要多个服务器(用于容错、负载平衡等)但只想处理每条消息一次的情况。共享订阅提供了一种实现此目的的简单方法。

Then, how the other clients of the group receive the message as they also subscribe to the same topic?

如果他们订阅相同的共享订阅(具有相同的 ShareName),则只有一个人会收到消息;这是设计使然。如果您希望他们都收到消息,请不要使用共享订阅。或者,您可以根据 the spec:

建立多个订阅(这样所有订阅者都会收到消息,但只有一个订阅者会处理它 - 但请注意“可以”)

If a Client has a Shared Subscription and a Non‑shared Subscription and a message matches both of them, the Client will receive a copy of the message by virtue of it having the Non‑shared Subscription. A second copy of the message will be delivered to one of the subscribers to the Shared Subscription, and this could result in a second copy being sent to this Client.