MQTT (or Message Queue Telemetry Transport) is not a new protocol. In fact, the first version of the protocol was authored by Any Stanford-Clark and Arlen Nipper of Cirrus Link in 1999, but ,owing to its lightweight nature and small code footprint, it has recently found its home in the Internet of Things.
MQTT is an ISO standard (ISO/IEC PRF 20922) publish-subscribe-based "lightweight" messaging protocol for use on top of the TCP/IP protocol. It is designed for connections with remote locations where a "small code footprint" is required or the network bandwidth is limited. The publish-subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message.
In the publish–subscribe model, subscribers typically receive only a subset of the total messages published. The process of selecting messages for reception and processing is called filtering. There are two common forms of filtering: topic-based and content-based.
In a topic-based system, messages are published to "topics" or named logical channels. Subscribers in a topic-based system will receive all messages published to the topics to which they subscribe, and all subscribers to a topic will receive the same messages. The publisher is responsible for defining the classes of messages to which subscribers can subscribe.
In a content-based system, messages are only delivered to a subscriber if the attributes or content of those messages match constraints defined by the subscriber. The subscriber is responsible for classifying the messages.
Some systems support a hybrid of the two; publishers post messages to a topic while subscribers register content-based subscriptions to one or more topics.
Messages are the information that you want to exchange between your devices. Whether it’s a command or data.
In MQTT, a publisher publishes a message to a Topic, and the message is received by the subscribers subscribed to that Topic
Another important concept is topics. Topics are the way you register interest for incoming messages or how you specify where you want to publish your message.
Topics are represented with strings separated by slashes. The slashes indicate the topic level.
The broker is primarily responsible for receiving all messages, filtering the messages, decide who is interested in it and then publishing the message to all subscribed clients.
Quality of Service(QoS)
Quality of Service (QoS) is a rather broad topic in general. For MQTT though, QoS takes a simple form. It allows an application (= a client publishing or subscribing to a topic) to choose one of the following transport guarantees:
A client won't receive any confirmation from the broker upon receipt. Likewise a message delivered to a client from the broker is not required to be acknowledged. This is the fastest way to publish and receive messages, but also the one where message loss is most likely to happen.
A client will receive a confirmation message from the broker upon receipt. If the expected confirmation is not received within a certain time frame, the client has to retry the message. A message received by a consumer must be acknowledged on time as well, otherwise the broker will re-deliver the message.
The first part of the QoS 2 flow is similar to QoS 1. A second flow ensures that sender and receiver (broker) agree on where the message stands. The point is to avoid duplicate routing, i.e. delivery to the subscribers downstream of the broker, as the broker can recognize duplicates itself. QoS 2 doesn't necessarily imply that the publisher will only send the message once, it will still retry if it gets no acknowledgements from the broker.