MQTT, Message, Protocol, Topic, Subscribe, Publish → AWS IoT Core, Azure, Google, Smartthings IoT
MQTT Message Protocol
It is one of the message protocols required to send and receive messages between IoT devices.
MQTT protocol can send and receive messages many-to-many through a logical channel called “topic”,
MQTT is based on TCP and SSL/TLS can be applied for security.
→ a message protocol, CoAP is based on UDP, and DTLS can be used for security.
1. Operators in MQTT
There are roles of Broker, Publisher, and Subscriber in MQTT.
It relays, publishes messages, and subscribes to messages for a certain topic for each role.
MQTT Broker plays a key role in relaying messages to IoT Core, and IoT devices will mainly be the subjects of publishing and subscribing to messages.
Broker | Relaying the message, Broker Example) Mosquitto, HiveMQ, RabbitMQ, 등 |
Publisher | A device that publishes a message to a topic |
Subscriber | A device that subscribes to messages on a topic |
2. Format of MQTT topic
A topic is a target that publishes and subscribes to messages and is expressed in a hierarchical structure.
The way of expression is as follows.
To give an example by referring to the table below,
if you want to open the window in the living room of our home, you can issue an open message with the topic “smart-home/livingroom/window”.
Look, is the window open?
Hierarchical Structure | Expression as Topic | ||
smart-home | livingroom | window | smart-home/livingrootm/window |
light | smart-home/livingrootm/light | ||
room1 | humidity | smart-home/room1/humidity | |
temperature | smart-home/room1/Temperature |
3. MQTT message/packet structure
MQTT message format is divided into a fixed header(2 bytes), an optional header(variable length), and a payload((variable length)) as shown below.
4. MQTT Message Type
Message Type | Direction of flow | Description |
CONNECT | Client to Server | Client request to connect to Server |
CONNACK | Server to Client | Connect acknowledgment |
PUBLISH | Client to Server or Server to Client | Publish message |
PUBACK | Client to Server or Server to Client | Publish acknowledgment |
PUBREC | Client to Server or Server to Client | Publish received (assured delivery part 1) |
PUBREL | Client to Server or Server to Client | Publish release (assured delivery part 2) |
PUBCOMP | Client to Server or Server to Client | Publish complete (assured delivery part 3) |
SUBSCRIBE | Client to Server | Client subscribe request |
SUBACK | Server to Client | Subscribe acknowledgment |
UNSUBSCRIBE | Client to Server | Unsubscribe request |
UNSUBACK | Server to Client | Unsubscribe acknowledgment |
PINGREQ | Client to Server | PING request |
PINGRESP | Server to Client | PING response |
DISCONNECT | Client to Server | Client is disconnecting |