Fundamentals 6 min read

Understanding MQTT Publish and Subscribe: Message Formats, QoS Levels, Retain Flags, and Acknowledgments

This article explains how MQTT handles message publishing and subscribing, covering topic structures, QoS levels, retain flags, payload details, duplicate fields, subscription formats, packet identifiers, subscription acknowledgments, and unsubscribe procedures, providing a comprehensive overview of the protocol's messaging workflow.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
Understanding MQTT Publish and Subscribe: Message Formats, QoS Levels, Retain Flags, and Acknowledgments

Message Publishing

Each MQTT message must contain a topic, which the server uses to deliver the message to interested clients. The payload is transmitted as binary data, and MQTT is agnostic to the content format, allowing binary, text, XML, JSON, etc.

Publish Message Format

Topics are hierarchical strings separated by slashes, e.g., "myhome/livingroom/temperature".

Quality of Service (QoS)

QoS determines delivery guarantees: 0 – at most once, no retry; 1 – at least once, retries until ACK; 2 – exactly once, ensuring a single delivery.

Retain Flag

The retain flag indicates whether the broker should store the message as the latest retained message for the topic, which is sent to new subscribers; only one retained message per topic is kept.

Message Payload

The payload holds the actual data; MQTT does not interpret it, so any format can be transmitted.

Duplicate Flag

When QoS > 0 and a message is retransmitted, the duplicate flag is set to inform the receiver that this may be a retry.

Message Subscription

If no client is subscribed to a topic, published messages are discarded. Clients send a SUBSCRIBE request to the broker for one or more topics, each with a desired QoS; topics may contain wildcards, and overlapping subscriptions are resolved by the highest QoS.

Subscribe Message Format

Each subscription includes a unique packet identifier and a list of topic‑QoS pairs.

Subscription Acknowledgment (SUBACK)

The broker replies with a SUBACK containing the same packet identifier and a return code for each requested QoS (0, 1, 2 for success, 0x80 for failure).

Unsubscribe

Clients can cancel subscriptions by sending an UNSUBSCRIBE packet that includes a unique identifier and the list of topics to remove.

Unsubscribe Acknowledgment (UNSUBACK)

The broker returns an UNSUBACK with the same packet identifier; it is sent regardless of whether the topics were previously subscribed.

Summary

MQTT message exchange works by publishing to topics and subscribing to those topics; the protocol abstracts away topic creation and decouples publishers from subscribers, enabling flexible patterns such as grouping and broadcasting, while QoS and retain flags provide delivery control.

IoTProtocolQoSMQTTPublish-SubscribeMessage QueuingRetain Flag
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.