MQTT Topics are UTF-8 strings structured in a hierarchy similar to folders and files in a file system using the forward slash ( / )as a delimiter.
To be valid a topic must be at least 1 character long and generally in real world applications they 10s of characters in length.
The topic is sent with every message published, and so in order to reduce the length of published messages MQTT-SN uses shorter topic names.
it does this by using a variety of topic identifiers in place of standard topic name used in MQTT.
In MQTT-SN there are 4 possible topic Identifiers that can be used.
- Topic names
- Topic-ids
- Short topic names (2 characters)
- Pre-defined topic IDs.
Topic Names
As per MQTT 3.1 a topic name is a UTF8-encoded string that can include wild card characters + and #.
When you subscribe to a topic using a topic name the subscribe functions returns a topic ID for this topic name.
The topic ID can then be used to publish messages on this topic.
Topic names cannot be used to publish messages!
Topic IDs
A topic ID is a two byte field with 0x0000x and 0xFFFF being reserved.
A topic ID can be obtained from a broker or client using the register mechanism and also from a broker using a subscribe message.
Topic IDs can be used for publishing messages.
Although you can use a topic ID to subscribe it is not normally done as you must first find the topic id using the registration process.
Short Topic Names
This is a two character name e.g. “AA” .They are very useful as you can also publish on this topic without requiring topic registration.
Short topic names can be used for both publishing and subscribing
Predefined Topic IDs
Topics IDs can be predefined on both the gateway and client.
Predefined topic names also don’t require topic registration, but they do require a topic list.
For a client to subscribe or publish to a predefined topic e.g. topic1 then this topic must be defined on the broker in a predefined topic list.
Predefined topics aren’t currently supported on RSMB but they are supported on the paho Gateway.
The subscribe message contains a flag to differentiate between a short topic name and a predefined topic ID.
Registering Topic Names
The registration process in MQTT-SN is a way of obtaining a topic id that can be used for publishing messages.
A registration message contains the full topic name as per MQTT and the response is a 16 bit topic ID.
The register message can be sent from either the client or gateway/broker.
Common Questions and Answers
Q- Can I use wild card characters in MQTT-SN topics?
A- Yes but only when subscribing using topic names.
Summary
Topic names have been replaced with other topic identifiers in an attempt to keep the message overhead small.
Although topic names can be used when subscribing they cannot be used for publishing messages.
When publishing messages a topic Id, short topic name or pre-defined topics must be used.
Related Tutorials and resources:
- MQTT-SN RSMB Broker Overview,Install and Configuration
- Using The Python MQTT-SN Client
- MQTT-SN Publish and Subscribe Test Tools
- Publish and Subscribe Using the Python MQTT-SN Client
- MQTT-SN Gateway Advertisement and Discovery
- MQTT-SN Topic Names and Identifiers