Understanding wildcards
When we analyzed the subscription operation, we learn that an MQTT client can subscribe to one or more topic filters. In case we specify a topic name as a topic filter, we will only subscribe to a single topic. We can take advantage of the following two wildcards to create topic filters that subscribe to all the topics that match the filter:
- Plus sign (
+
): It is a single level wildcard that matches any name for a specific topic level. We can use this wildcard instead of specifying a name for any topic level in the topic filter. - Hash (
#
): It is a multi level wildcard that we can use only at the end of the topic filter, as the last level and matches any topic whose first levels are the same as the topic levels specified at the left-hand side of the#
symbol.
For example, in case we want to receive all the messages related to the altitudes for all the drones, we can use the +
single level wildcard instead of a specific drone name. We can use the following topic filter: sensors/+/altitude
.
If we publish messages to the following topics, the subscriber that used the sensors/+/altitude
topic filter will receive all of them:
sensors/drone01/altitude
sensors/drone02/altitude
sensors/superdrone01/altitude
sensors/thegreatestdrone/altitude
The subscriber to the sensors/+/altitude
topic filter won't receive messages sent to any of the following topics, because they won't match the topic filter.
sensors/drone01/speed/rotor/1
sensors/superdrone01/speed/rotor/2
sensors/superdrone01/remainingbattery
In case we want to receive all the messages related to all the sensors for the drone named drone01
, we can use the #
multi level wildcard after the drone name and the slash (/
). We can use the following topic filter: sensors/drone01/#
.
If we publish messages to the following topics, the subscriber that used the sensors/drone01/#
topic filter will receive all of them:
sensors/drone01/altitude
sensors/drone01/speed/rotor/1
sensors/drone01/speed/rotor/2
sensors/drone01/speed/rotor/3
sensors/drone01/speed/rotor/4
sensors/drone01/remainingbattery
We used a multi level wildcard, and therefore, no matter the additional topic levels after sensors/drone01/
, we will receive all of them.
The subscriber to the sensors/drone01/#
topic filter won't receive messages sent to any of the following topics, because they won't match the topic filter. None of the following has sensors/drone01/
as a prefix, and therefore, they don't match the topic filter.
sensors/drone02/altitude
sensors/superdrone01/altitude
sensors/thegreatestdrone/altitude
sensors/drone02/speed/rotor/1
sensors/superdrone02/speed/rotor/2
sensors/superdrone02/remainingbattery
Obviously, we must be careful when we use any wildcard because we might be subscribing to a huge number of topics with a single topic filter. We have to avoid subscribing to topics aren't of interest to the client to avoid wasting unnecessary bandwidth and the broker server resources.
We will use these wildcards in subscriptions later to analyze how different QoS levels work with MQTT.