TsgcWSPClient_MQTT | MQTT Topics

Topics

In MQTT, the word topic refers to an UTF-8 string that the broker uses to filter messages for each connected client. The topic consists of one or more topic levels. Each topic level is separated by a forward slash (topic level separator)

 

myHome / groundfloor / livingroom / temperature

 

In comparison to a message queue, MQTT topics are very lightweight. The client does not need to create the desired topic before they publish or subscribe to it. The broker accepts each valid topic without any prior initialization. Note that each topic must contain at least 1 character and that the topic string permits empty spaces. Topics are case-sensitive.

 

 

WildCards

When a client subscribes to a topic, it can subscribe to the exact topic of a published message or it can use wildcards to subscribe to multiple topics simultaneously. A wildcard can only be used to subscribe to topics, not to publish a message. There are two different kinds of wildcards: _single-level and _multi-level.

 

Single Level: +

As the name suggests, a single-level wildcard replaces one topic level. The plus symbol represents a single-level wildcard in a topic.

 

myHome / groundfloor / + / temperature

 

Any topic matches a topic with single-level wildcard if it contains an arbitrary string instead of the wildcard. For example a subscription to _myhome/groundfloor/+/temperature can produce the following results:

 

YES => myHome / groundfloor / livingroom / temperature

YES => myHome / groundfloor / kitchen / temperature

NO   => myHome / groundfloor / livingroom / brightness

NO   => myHome / firstfloor / livingroom / temperature

NO   => myHome / groundfloor / kitchen / fridge / temperature

 

Multi Level: #

The multi-level wildcard covers many topic levels. The hash symbol represents the multi-level wild card in the topic. For the broker to determine which topics match, the multi-level wildcard must be placed as the last character in the topic and preceded by a forward slash.

 

myHome / groundfloor / #

 

YES => myHome / groundfloor / livingroom / temperature

YES => myHome / groundfloor / kitchen / temperature

YES => myHome / groundfloor / kitchen / brightness

NO   => myHome / firstfloor / kitchen / temperature

 

When a client subscribes to a topic with a multi-level wildcard, it receives all messages of a topic that begins with the pattern before the wildcard character, no matter how long or deep the topic is. If you specify only the multi-level wildcard as a topic (_#), you receive all messages that are sent to the MQTT broker.