Main Content

Publish MQTT Messages and Subscribe to Message Topics

Message Queuing Telemetry Transport (MQTT) is a publish-subscribe architecture that is developed primarily to connect bandwidth and power-constrained devices over wireless networks. It is a simple and lightweight protocol that runs over TCP/IP sockets, WebSockets, and SSL (secure sockets layer).

MQTT has two components:

  • MQTT broker — An MQTT broker is a central point of communication. The broker is responsible for dispatching all messages between the clients.

  • MQTT client — An MQTT client is any device (for example, a computer or a mobile phone) that connects to the broker. A client that sends messages is a publisher. A client that receives messages is a subscriber. To receive a message, the client must subscribe to the topic of that message.

MQTT architecture

Topics in MQTT

A topic is an identifier (ID) used by the MQTT broker to identify rightful clients for delivering messages. Each client that wants to send messages publishes them on a certain topic, and each client that wants to receive messages subscribes to a certain topic.

A topic is a string and can consist of one or more topic levels. Each level is separated by a forward slash (/), for example, home/livingroom/temperature.

A topic:

  • Must have at least one character.

  • Is case-sensitive. For example, home/room/temperature and home/Room/temperature are two different topics.

Wildcards in MQTT Topics

Wildcards are special characters in a topic used by the clients to subscribe to multiple topics. MQTT supports single-level and multi-level wildcards.

  • Single-level wildcard: A single-level wildcard is represented using a plus sign (+). For a client to receive messages, all the levels of the subscribed topic, except the level with a + sign, must match the topic of the incoming message. You can include more than one single-level wildcard in a topic string.

  • Multi-level wildcard: A multi-level wildcard is represented using a number sign (#). Client receives message from all the sub-levels of the subscribed topic. You can include only one multi-level wildcard, and it must be at the end of the topic string.

    Wildcard in TopicClient Subscribed TopicMatchesDoes Not Match
    Single-level wildcardhome/floor1/+/temperature
    • home/floor1/livingroom/temperature

    • home/floor1/kitchen/temperature

    In this example, the wildcard + is replaced by livingroom and kitchen. All other levels match the client subscribed topic. The client receives messages from these topics.

    • home/floor1/study/brightness

    • home/floor1/livingroom/temperature/sensor2

    In this example, the wildcard + is replaced by study and livingroom. Other levels do not match the client subscribed topic. The client does not receive messages from these topics.

    Multi-level wildcard (All the sublevels)home/floor1/#
    • home/floor1/livingroom/humidity

    • home/floor1/livingroom/temperature

    • home/floor1/kitchen/temperature

    In this example, the wildcard # is replaced by livingroom/humidity, livingroom/temperatureand kitchen/temperature. All other levels match the client subscribed topic. The client receives messages from these topics.

    • home/floor2/livingroom/humidity

    • Home/floor2/kitchen/humidity

    In this example, the wildcard, # is replaced by livingroom/humidity and kitchen/humidity. Other levels do not match the client subscribed topic. The client does not receive messages from these topics.

Levels of QoS in MQTT

Quality of Service (QoS) defines the reliability of the message delivery process in MQTT. MQTT provides three QoS levels for message delivery: QoS 0, QoS 1, and QoS 2. You can have different QoS levels for publishing and for subscribing to messages. The MQTT broker that you are using might not support all three levels of QoS. For example, ThingSpeak™ MQTT supports only QoS 0.

QoS 0 — At Most Once

QoS level 0, often called "fire and forget" is a best effort delivery. There is no guarantee of delivery. The client does not acknowledge the receipt of the message. The MQTT broker does not retry and deletes the message locally.

MQTT QoS Level 0

QoS 1 — At Least Once

QoS level 1 guarantees that a message is delivered at least once. The MQTT broker sends the message to the client at least once. The MQTT broker stores the message until it receives a publish acknowledgement (PUBACK) packet from the client. If no acknowledgment is received after 10 seconds, the broker resends the message. In this level, the same message might be sent or delivered multiple times.

MQTT QoS Level 1

QoS 2 — Exactly Once

QoS 2 is the highest level of service in MQTT and guarantees each message is received only once by the intended recipients. QoS level involves additional overhead (four-step handshake) and is the slowest of all service levels.

When a client gets a QoS 2 packet from the broker, it processes the message and replies with a publish acknowledgement (PUBREC) packet. If the broker does not get the PUBREC packet, it resends the PUBLISH packet until it receives an acknowledgement. With the PUBREC acknowledgment, the broker discards the initial PUBLISH packet. Then, the broker stores the PUBREC packet and responds with a publish discarded (PUBREL) packet.

Once the client gets the PUBREL packet, it can discard all stored states and respond with a publish complete (PUBCOMP) packet. The broker can now delete the stored PUBREC packet and the packet identifier is available for reuse.

MQTT QoS Level 2