Publish/Subscribe Model

The publish/subscribe model is different from the traditional client/server model. It separates the client (publisher) that sends the message from the client (subscriber) that receives the message, and there is no need to build a direct connection between the publisher and the subscriber.

The MQTT protocol adopts this publish/subscribe model and routes messages based on topics rather than content. Each message contains a topic, and the agent does not need to parse user data. This provides the possibility to implement a general-purpose, business-independent MQTT agent.

MQTT topics (Topic) are similar to URLs, for example:

  1. chat/room/1
  2. sensor/10/temperature
  3. sensor/+/temperature
  4. $SYS/broker/metrics/packets/received
  5. $SYS/broker/metrics/#

Topic (Topic) is separated by / and supports wildcards like + and #:

  1. '+': Indicates a wildcard level, such as a/+, matches a/x, a/y
  2. '#': Indicates multiple levels of wildcarding, such as a/#, matches a/x, a/b/c/d

Subscribers and publishers communicate to each other via topic routing messages, for example, when using the MQTTX CLI command line to publish subscription messages:

  1. mqttx pub -t a/b/+ -q 1
  2. mqttx sub -t a/b/c -m hello -q 1

Subscribers can subscribe to topics containing wildcards, but publishers are not allowed to publish messages to topics containing wildcards.

TIP

For more information about MQTT publish-subscribe model and MQTT topics, please refer to: