Search code examples
mqtt

Can the + wildcard match 0 levels?


I'm confused on one bit of the MQTT specification regarding the + wildcard. Can + be used to match 0 levels?

For example, consider a subscriber on +/b/c. I believe it should get messages sent to a/b/c, x/b/c, y/b/c, and even /b/c. However, should it also get messages sent to b/c (i.e. no leading /)?


Solution

  • No + will only match a whole level of topic hierarchy, it will not (and should not) match b/c in the example given.

    From the 5.0 spec:

    4.7.1.3 Single-level wildcard

    The plus sign (‘+’ U+002B) is a wildcard character that matches only one topic level.

    The relevant part being the "matches only one topic level", not "matches zero or one topic level"

    p.s. please don't use leading / on topics while technically legal in the spec, it inserts a null (e.g. <null>/b/c hence how it matches the pattern in the question) in the topic hierarchy, which is a pain to parse and breaks things like shared subscriptions.