Understand MQTT topics

The MQTT topic is an addressing method that allows MQTT clients to share information.

The structure of MQTT topics is similar to folders and files in the file system, using forward slashes (/) as the separator.

Using this system, you can create a user-friendly and self-describing naming structure of your choice.

Subject name:

  • case sensitive
  • Use UTF-8 strings.
  • It must contain at least one character to be valid.

$ SYS theme

Except for the $SYS theme, there is no default theme or standard theme structure.

By default, no topics will be created on the server (broker) except for the $SYS topic.

All topics are created by subscribing or publishing clients and are not permanent.

The topic only exists if the client has subscribed to the topic, or the server (broker) has stored reserved messages or last will messages for the topic.

The $SYS topic is a reserved topic, and most MQTT servers (broker) use it to publish information about the server (broker).

They are read-only topics for MQTT clients. There is no standard for the topic structure, but there is a guiding principle , which most server (broker) implementations seem to follow.

Subscribe to topics


Customers can subscribe to single or multiple topics.

When subscribing to multiple topics, two wildcards can be used. they are:

# (Hash character)-multi-level wildcard
  + (plus sign)-single-level wildcard
wildcard can only be used to represent one or more levels, such as /house/#, and cannot be used to represent a name with multiple characters A part, such as hou#, is invalid.

Theme naming example

Active topic subscription


Subscribe to a single topic

  • /
  • /house
  • house/room/main-light
  • house/room/side-light

Use theme wildcards


Subscribe to topic house/#

include

  • house/room1/main-light
  • house/room1/alarm
  • house/garage/main-light
  • house/main-door
  • etc

Subscribe to topic house/+/main-light

include

  • house/room1/main-light
  • house/room2/main-light
  • house/garage/main-light

But not including

  • house/room1/side-light
  • house/room2/side-light

Invalid topic subscription

house+-reason-no subject level
house#-reason-no subject level

Publish topic


Customers can only post to a single topic. In other words, wildcards are not allowed when publishing.

For example, to publish a message to two topics, you need to publish the message twice.

When to create a theme


Dynamically create topics in the following situations:

  • Someone subscribed to a topic
  • Someone publishes a message to the topic and sets the retained message to True.

When to delete topics from the server (broker)

  • When the last client subscribed to the server (broker) disconnects and clears the session is set to true.
  • When the client connects, clear the session is set to True.

Republish topic data


This is most likely to happen when changing or combining the naming scheme.

The idea is that the customer will subscribe to a topic, such as hub1/sensor1, and republish the data with the new topic named house1/main-light.

Frequently Asked Questions


Q: How can I subscribe to all topics?
Answer: Subscribe#

Q: How can I subscribe to all $SYS topics?
Answer: Subscribe to $SYS/#

Q: Should I start my topic hierarchy with /?
Answer: No, it just adds another level to the structure.

Q: Can I get a list of all topics on the server (broker)?
A: Unless you subscribe to all topics and scan, you can't.

Q: Can I tell who has subscribed to a topic?
Answer: No

Q: How do I discover the subject?
Answer: Currently, there is no mechanism other than listing all the topics.

 

http://www.steves-internet-guide.com/understanding-mqtt-topics/

Guess you like

Origin blog.csdn.net/maimang1001/article/details/109246215