MQTT is a lightweight publish/subscribe messaging protocol built on TCP/IP. Originally designed for monitoring oil pipelines (as MQ Telemetry Transport), MQTT is now the standard messaging and data exchange protocol for the Internet of Things.
An MQTT client is any device that runs an MQTT library and connects to an MQTT broker (server) over a network. Unlike in the traditional client-server model where clients communicate directly with an endpoint, in the publish/subscribe ("pub/sub") model, clients communicate through a central broker. The client that sends a message (the publisher) and the client that receives that message (the subscriber) never interact. The broker filters incoming messages from the publisher and distributes them to the subscriber(s).
Messages are filtered using topics. A topic is a UTF-8 string that the broker uses to to decide which subscriber receives which message. The topic consists of one or more topic levels, with each topic level separated by a forward slash (topic level separator). A topic must be at least one character long and topic names are case-sensitive.
For example, in the topic events/object/occupantTemperature
, the first level is "events", the second level is "object", and the third level is "occupantTemperature".
When a client subscribes to a topic, it can subscribe to the exact topic of a published message or it can use wildcards to subscribe to multiple topics at once. There are two types of wildcards: single-level (+) and multi-level. The single-level wildcard replaces one topic level, while the multi-level wildcard replaces multiple topic levels. The multi-level wildcard must always be the last character in the topic string and must be preceded by a forward slash.
For example: events/object/#
Wildcards cannot be used when publishing to a topic.
Quality of Service
Senders and receivers set a Quality of Service (QoS) level that defines how much effort will be expended to ensure that the message is delivered.
There are three QoS levels in MQTT:
Level 0 | The message is delivered at most once, according to the best efforts of the underlying TCP/IP network. There is no guarantee of delivery. You can think of this approach as send and forget. |
Level 1 | The message is delivered at least once, with multiple retries until the message is acknowledged as received. You can think of this approach as acknowledged delivery. |
Level 2 | The message is delivered exactly once, with the sender and receiver doing a handshake to ensure that the message is received once by the intended recipient. You can think of this approach as guaranteed delivery. |
Both the subscriber and publisher can set a QoS level. If the levels don't match, the lower of the two is used. For example, if a client subscribes to a message at a QoS of 1 but the message was published at a QoS of 0, then the client will receive the message at a QoS of 0.
The content of a message is referred to as the payload. For example, the payload of a subscribe message will contain a list of topic names to which the client wants to subscribe, as well as the QoS level at which the client wants to receive the messages.
For more information about MQTT, go to https://mqtt.org.
Comments
0 comments
Please sign in to leave a comment.