0
0
IOT Protocolsdevops~6 mins

Wildcard subscriptions (+ and #) in IOT Protocols - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine trying to listen to many different conversations at once without knowing exactly which ones will happen. Wildcard subscriptions solve this problem by letting you listen to groups of messages without specifying each one individually.
Explanation
Single-level wildcard (+)
The plus sign (+) acts as a placeholder for exactly one topic level in a subscription. It matches any single word in that position but does not cross over multiple levels. This allows you to subscribe to messages where only one part of the topic varies.
The + wildcard matches exactly one topic level in a subscription.
Multi-level wildcard (#)
The hash sign (#) matches any number of topic levels, including zero. It must be the last character in the subscription and covers all subtopics below that point. This lets you listen to an entire branch of topics without listing each one.
The # wildcard matches zero or more topic levels and must be last in the subscription.
Usage rules and placement
The + wildcard can appear anywhere in the topic filter but only replaces one level. The # wildcard can only appear at the end of the topic filter and covers all remaining levels. Using these wildcards correctly ensures you receive the intended messages without errors.
+ can be used anywhere for one level; # only at the end for multiple levels.
Practical benefits
Wildcard subscriptions reduce the need to subscribe to many individual topics. They simplify managing dynamic or large topic trees by grouping related messages. This makes it easier to handle data streams in IoT and messaging systems.
Wildcards simplify subscribing to many related topics efficiently.
Real World Analogy

Imagine you want to listen to all announcements in a building. The + wildcard is like listening to any room on a specific floor, while the # wildcard is like listening to all rooms on all floors from a certain point down.

Single-level wildcard (+) → Listening to any single room on a specific floor
Multi-level wildcard (#) → Listening to all rooms on all floors below a certain floor
Usage rules and placement → Rules about where you can stand to listen and what rooms you can hear
Practical benefits → Easier to hear many announcements without moving to each room
Diagram
Diagram
Topic Tree Example:

home
├─ kitchen
│  ├─ temperature
│  └─ humidity
├─ livingroom
│  ├─ temperature
│  └─ light
└─ garage
   └─ door

Subscriptions:

home/+/temperature  → matches home/kitchen/temperature and home/livingroom/temperature
home/#              → matches all topics under home, like home/garage/door
This diagram shows a topic tree and how + and # wildcards match different topic levels.
Key Facts
Single-level wildcard (+)Matches exactly one topic level in a subscription filter.
Multi-level wildcard (#)Matches zero or more topic levels and must be the last character in the filter.
Wildcard placement rule+ can appear anywhere; # only at the end of the topic filter.
Subscription filterA pattern used to receive messages from matching topics.
Topic levelA segment of a topic name separated by slashes (/).
Common Confusions
Believing # can be used in the middle of a topic filter.
Believing # can be used in the middle of a topic filter. The # wildcard must be the last character in the subscription filter and cannot appear in the middle.
Thinking + matches multiple topic levels.
Thinking + matches multiple topic levels. The + wildcard matches exactly one topic level, not multiple levels.
Assuming wildcards match topic names themselves.
Assuming wildcards match topic names themselves. Wildcards match topic levels in subscriptions, not the actual topic names published.
Summary
The + wildcard matches exactly one topic level anywhere in the subscription filter.
The # wildcard matches zero or more topic levels but must be the last character in the filter.
Using wildcards helps efficiently subscribe to many related topics without listing each one.