MQTT is a lightweight message transport protocol developed in 1999. Since this protocol is very lightweight and bandwidth-efficient, it is best suitable for IoT applications with different wireless sensor network communications that are involved.
It follows the PubSub model which is a publisher-subscriber model that is entirely different from the HTTP protocol’s Request/Response model. One of the key features of MQTT is that it is an asynchronous protocol. The client need not be connected with the broker for the operation.
- Client: A device that publishes messages on a particular topic (publisher) or receives messages by subscribing to a particular topic (subscriber). They are connected to a central server where they maintain persistent connectivity or transiently.
- Broker: The server which receives forwarding the messages sent by the publishers to all the subscribers and maintains a connection with clients.
- Topic: A namespace/string for identifying messages to the broker/client. It is an endpoint used to address different data received by the broker and requested by subscribers.
- Publish: Action of sending/dispatching the data to the broker
- Subscribe: Client action indicating the interest in receiving a topic updates
In the PubSub model, the clients don’t connect directly, instead, they are connected through a broker. The communication between publisher and subscribers are not directly either and the broker acts as a communication link between clients. Clients send messages by specifying a topic. Subscriber clients subscribe to interesting topics and messages from that topic are sent by the broker once they receive them. This model ensures that both subscriber and clients need not be connected all the time and directly each other, which enhances the security of each client.
MQTT Protocol Key features
- Lightweight message queueing and transport protocol
- Asynchronous communication model with messages (events)
- Low overhead (2 bytes header) for low network bandwidth applications
- Publish / Subscribe (PubSub) model
- Decoupling of data producer (publisher) and data consumer (subscriber) through topics (message queues)
- Simple protocol, aimed at low complexity, low power and low footprint implementations (e.g. WSN – Wireless Sensor Networks)
- Runs on connection-oriented transport (TCP). To be used in conjunction with 6LoWPAN (TCP header compression)
- MQTT caters for (wireless) network disruptions
- It is over TCP.
- It uses SSL/TLS for security.
- There are many messages used in MQTT protocol such as CONNECT, PUBLISH, SUBSCRIBE, DISCONNECT, etc.
- Username/Password is used in the ‘connect’ message.
- It encrypts payload i.e. it is payload agnostic.
- 1883 port is used by the MQTT protocol. For TLS/SSL, it is 8883 by default.