MQTT Protocol Overview
MQTT protocol is a power-saving M2M protocol widely used in Internet of things. MQTT protocol is used to send and receive data from smart objects.This protocol used publish-subscriber paradigm in contrast to HTTP based on request/response paradigm. It uses binary messages to exchange information with a low overhead. MQTT uses TCP stack as transmission substrate.
Message Pattern
MQTT protocol implements publish-subscriber paradigm. This paradigm decouples a client that publishes a message (“publisher”) to other clients that receive the message (“subscribers”). Moreover, MQTT is asynchronous protocol, that means that it does not block the client while it waits for the message. In contrast to HTTP protocol, that is mainly a synchronous protocol. Another interesting property of MQTT protocol is that it does not require that the client (“subscriber”) and the publisher are connected at the same time.MQTT Publisher-Subscriber Pattern (MQTT Broker, MQTT Client)
The key component in MQTT is the MQTT broker, which dispatches messages to the MQTT clients (“subscribers”). In other words, the MQTT broker receives messages from publisher and dispatches these messages to the subscribers. While it dispatches messages, the MQTT broker uses the topic to filter the MQTT clients that will receive the message.A topic is a virtual channel that connects a publisher to its subscribers. This topic is managed by the MQTT broker. Through this virtual channel, the publisher is decoupled from the subscribers and the MQTT clients (publishers or subscribers) do not have to know each other to exchange data. This makes this protocol highly scalable without a direct dependency from the message producer (“publisher”) and the message consumer (“subscriber”).
The schema below describes the MQTT architecture:
How To Use MQTT Protocol
There are several implementations of MQTT, for example, Mosquitto, an implementation developed by Eclipse. The first step is installing the MQTT broker.
The MQTT server (aka MQTT broker) is installed on Raspberry Pi. This server is our MQTT broker. Then we need to install the client, or in other words, the publisher and the subscriber. You can install client and server on the same Raspberry, or install it on a different pc/server or IoT board.
How To Send An MQTT Message
We have installed and configured the client and the server, now we can register a subscriber to a specific topic and wait for an incoming message from a publisher.