What if you could listen to hundreds of devices with just one simple subscription?
Why Wildcard subscriptions (+ and #) in IOT Protocols? - Purpose & Use Cases
Imagine you have hundreds of sensors sending data to your system, each with a unique topic name. You want to listen to many of these topics, but you have to subscribe to each one manually, typing out every single topic name.
This manual approach is slow and tiring. If a new sensor is added, you must update your subscriptions again. It's easy to make mistakes, miss topics, or overload your system with too many subscriptions.
Wildcard subscriptions let you listen to many topics with just one subscription. Using '+' and '#' symbols, you can match multiple topics at once, saving time and reducing errors.
subscribe('home/kitchen/temperature') subscribe('home/livingroom/temperature') subscribe('home/garage/temperature')
subscribe('home/+/temperature') subscribe('home/#')
With wildcard subscriptions, you can easily scale your system to handle many devices without changing your code constantly.
A smart home app uses 'home/+/temperature' to get temperature updates from all rooms without listing each room separately.
Manual topic subscriptions are slow and error-prone.
Wildcards '+' and '#' simplify listening to many topics.
This makes IoT systems easier to manage and scale.
Practice
+ represent in topic subscriptions?Solution
Step 1: Understand the
The+wildcard meaning+wildcard matches exactly one topic level in MQTT subscriptions.Step 2: Compare with other wildcards
The#wildcard matches zero or more levels, so it is different from+.Final Answer:
It matches exactly one topic level. -> Option BQuick Check:
+= one level [OK]
- Confusing + with # wildcard
- Thinking + matches multiple levels
- Assuming + matches zero levels
home/kitchen including sub-levels?Solution
Step 1: Identify the wildcard for all sub-levels
The#wildcard matches all remaining topic levels, including zero or more sub-levels.Step 2: Apply to the topic
Usinghome/kitchenhome/kitchen/#subscribes tohome/kitchenand any deeper topics likehome/kitchen/temperature.Final Answer:
home/kitchen/# -> Option AQuick Check:
#matches all sub-levels [OK]
- Using + instead of # for multiple levels
- Placing # in the middle of the topic
- Using invalid syntax like # in the middle
building/+/temperature, which of these topics will be matched?Solution
Step 1: Understand the + wildcard position
The+wildcard matches exactly one topic level betweenbuildingandtemperature.Step 2: Check each topic against the pattern
Topicbuilding/floor1/temperaturematches becausefloor1fills the + wildcard. Others either have too many or too few levels or wrong last level.Final Answer:
building/floor1/temperature -> Option AQuick Check:
+matches one level only [OK]
- Matching topics with extra levels
- Ignoring the exact position of +
- Assuming + matches zero levels
sensor/#/temperature but it does not work. What is the problem?Solution
Step 1: Recall the rule for # wildcard placement
The#wildcard must be the last character in the topic filter and can only appear once.Step 2: Analyze the subscription
Here,sensor/#/temperature#is in the middle, which is invalid syntax and causes the subscription to fail.Final Answer:
The # wildcard must be at the end of the topic filter. -> Option DQuick Check:
#only at end [OK]
- Placing # in the middle of topic
- Confusing + and # usage
- Ignoring MQTT topic syntax rules
office but only one level deep, such as office/room1 and office/room2, but not office/room1/temperature. Which subscription should you use?Solution
Step 1: Understand the requirement for one level deep
You want to match topics exactly one level belowoffice, likeoffice/room1.Step 2: Choose the correct wildcard
The+wildcard matches exactly one level, sooffice/+matchesoffice/room1but notoffice/room1/temperature.Final Answer:
office/+ -> Option CQuick Check:
One level deep = use + wildcard [OK]
- Using # which matches all levels
- Using multiple + wildcards incorrectly
- Not including any wildcard
Ready to prove it?
Pass the test and this pattern moves unknown
