
Most implementations of MQTT purchasers use a callback mechanism to deal with incoming messages. Inside the callback perform, we solely have entry to the subject title of the message. If it’s a non-wildcard subscription, the subject filter used through the subscription shall be equivalent to the subject title within the message.
Due to this fact, we are able to instantly set up a mapping between the subscribed subjects and callback capabilities. Then, upon message arrival, we are able to lookup the corresponding callback primarily based on the subject title within the message and execute it.
Nevertheless, if it’s a wildcard subscription, the subject title within the message shall be totally different from the unique subject filter used through the subscription. On this case, we have to match the subject title within the message with the unique subscription one after the other to find out which callback perform must be executed. This impacts the processing effectivity of the shopper.
As well as, MQTT permits a shopper to determine a number of subscriptions, so a single message can match a number of shopper subscriptions when utilizing the wildcard subscription.
In such instances, MQTT permits the server to ship a separate message for every overlapping subscription or just one message for all of the overlapping subscriptions. The previous possibility implies that the shopper will obtain a number of duplicate messages.
No matter whether or not it’s the previous or latter possibility, the shopper can not decide which subscription(s) the message originated from. For instance, even when the shopper finds {that a} message matches two of its subscriptions, it can not assure that each subscriptions have been efficiently created when the server forwards the message to itself. Due to this fact, the shopper can not set off the right callback for the message.
How Does the Subscription Identifier Work?
To handle this subject, MQTT 5.0 launched Subscription Identifiers. Its utilization may be very easy: purchasers can specify a Subscription Identifier when subscribing, and the server must retailer the mapping relationship between the subscription and the Subscription Identifier. When a PUBLISH packet matches a subscription and must be forwarded to the shopper, the server will return the subscription identifier related to the subscription to the shopper along with the PUBLISH packet.
If the server chooses to ship separate messages for overlapping subscriptions, every PUBLISH packet ought to embody the Subscription Identifier that matches the subscription. If the server chooses to ship just one message for overlapping subscriptions, the PUBLISH packet will include a number of Subscription Identifiers.
The shopper solely wants to determine a mapping between Subscription Identifiers and callback capabilities. Through the use of the Subscription Identifier within the message, the shopper can decide which subscription the message originated from and which callback perform must be executed.
Within the shopper, the Subscription Identifier will not be a part of the session state, and its affiliation with any content material is totally decided by the shopper. Due to this fact, moreover callback capabilities, we are able to additionally set up mappings between Subscription Identifiers and subscribed subjects, or between Subscription Identifiers and the Consumer ID. The latter is especially helpful in gateway eventualities the place the gateway receives messages from the server and must ahead them to the suitable shopper. With the Subscription Identifier, the gateway can shortly decide which shopper ought to obtain the message with out re-matching and routing the subjects.
A SUBSCRIBE packet can solely include one Subscription Identifier. If a SUBSCRIBE packet consists of a number of subscriptions, the identical Subscription Identifier shall be related to all these subscriptions. So, please be certain that associating a number of subscriptions with the identical callback perform is intentional.