MQTT Topics
MQTT clients publish their contents using MQTT topics which are strings that is used by MQTT broker to filter the messages from MQTT clients.
MQTT Topic Levels
MQTT Topics can have one or more levels and the levels are separated by forward slash. Topics are case-sensitive. Examples of MQTT topics are,
- office/groundfloor/room1/temperature
- factory/device1/motorspeed
Related Topics
- How MQTT works?
- MQTT – Connect to Broker
- MQTT Topics
- MQTT Publish
- MQTT Subscribe
- MQTT Quality of Service (QoS)
- MQTT – Keep Alive with PINGREQ and PINGRESP
- Install MQTT broker Mosquitto in Windows
- Mosquitto Broker – Enable user authentication
- MQTT Broker connection with user name and password
MQTT Topic Wildcards
Clients can subscribe to MQTT topics using wildcards which cane be single-level or multi-level. Wildcards can only be used for subscribing to topics, not for publishing topics.
Single-level Wildcard: ‘+’ symbol is used to subscribe for single-level wildcards.
Example of single-level wildcard topic,
office/groundfloor/+/temperature
When clients subscribe to above topics with single-level topics, they will subscribe messages for below topics
office/groundfloor/room1/temperature
office/groundfloor/room2/temperature
office/groundfloor/room3/temperature
Multi-level Wildcard: ‘#’ symbol is used to subscribe for multi-level wildcards. They can be used only at the end of topics.
Example if multi-level wildcard topic,
office/groundfloor/room1/#
When clients subscribe to above topic with multi-level wildcard, they will receive all messages pubslished under offoce/groundfloor/room1
office/groundfloor/room1/temperature
office/groundfloor/room1/humidity
office/groundfloor/room1/noise
Try the AT Command Tester tool to create new topics and publish messages,
MQTT Server logs with examples of subscribing to topics
C:\Program Files\mosquitto>mosquitto -v
1556880402: mosquitto version 1.6.0 starting
1556880402: Using default config.
1556880402: Opening ipv6 listen socket on port 1883.
1556880402: Opening ipv4 listen socket on port 1883.
1556880410: New connection from 127.0.0.1 on port 1883.
1556880410: New client connected from 127.0.0.1 as at_tester_2305 (p2, c1, k60).
1556880410: No will message specified.
1556880410: Sending CONNACK to at_tester_2305 (0, 0)
1556880424: Received SUBSCRIBE from at_tester_2305
1556880424: SensorReadings (QoS 1)
1556880424: at_tester_2305 1 SensorReadings
1556880424: Sending SUBACK to at_tester_2305
1556880429: Received UNSUBSCRIBE from at_tester_2305
1556880429: SensorReadings
1556880429: at_tester_2305 SensorReadings
1556880429: Sending UNSUBACK to at_tester_2305
1556880462: Received SUBSCRIBE from at_tester_2305
1556880462: office/groundfloor/+/temperature (QoS 1)
1556880462: at_tester_2305 1 office/groundfloor/+/temperature
1556880462: Sending SUBACK to at_tester_2305
1556880490: Received SUBSCRIBE from at_tester_2305
1556880490: office/groundfloor/room1/# (QoS 1)
1556880490: at_tester_2305 1 office/groundfloor/room1/#
1556880490: Sending SUBACK to at_tester_2305
1556880550: Received PINGREQ from at_tester_2305
1556880550: Sending PINGRESP to at_tester_2305