How MQTT works?
MQTT works under a publish and subscribe model which makes the protocol possible to transfer messages to one or more clients. There is no direct link between the clients (sender and receiver). Therefore, the broker acts as a middleware between these clients. This protocol has a duplex communication (bidirectional). The client (sender) publishes messages on a topic and another client (receiver) is subscribed to the same topic to receive the messages in which the client (sender) publishing then. The publishers who send the messages and subscribers who receive the messages are MQTT clients.
MQTT specification defines the data transfer between controlled devices and server applications. It keeps bandwidth requirements to a minimum, handles unreliable networks, requires little implementation effort for developers, and is therefore ideal for machine-to-machine (M2M) communication. The protocol defines three QoS (Quality of service) levels, Last WILL & Retained Message, Flexible Subscription pattern, and MQTT Security for the device connectivity via SSL/ TLS and the protocol defined Authentication.
Central Message Broker
MQTT Broker is a central server that acts as a data warehouse for all the messages to be transmitted between the clients / Devices. Clients connect to the broker and subscries to the topics it requires to listen to for commands. Edge clients as well publish data when an event occurs on the sensors.
The broker keeps track of the subscriptions and published data. Based on the QoS levels, Retention definition and clean sessions defined by the clients, the broker sends the data to the respective clients. MQTT5 specification also defines the way the broker can handle the disconnection of clients and shared subscription.
Every device or edge application in the Message Queuing Telemetry Transport infrastructure must have the Client to read the edge data via sensors and communicate. Because the client is responsible for establishing a connection session with the MQTT server, providing subscriptions on topics of interest to them, and publishing information of interest to other clients. The clients only interact with the broker, as the clients can not connect to each other directly. Each client acts as both sender and receiver. Any device acts as the client that runs on an MQTT library and connects to the broker over the network.
IoT Applications Development
Two ways in building the IoT Application in MQTT.
Build over Broker & Build behind a Client
IoT Application over MQTT broker
90% of the application requirements today is the collection of data from various sources. As it is the data collection, the data will be sent to the central Broker. So it is better to build the application right behind the Broker where we get the most meta info of the data collected. As we build the application over the broker we cut down the flow of data from the broker to the application client. Python MQTT Interface of the MQTTRoute is one such Broker with flexible extendibility to build a complete application.
IoT Application behind MQTT client
In this method of IoT Application development, there will be a master client which subscribes to all the topics either one by one or using the # subscription. Due to the subscription, the client will receive all the data sent to the broker. The client will have mechanism to store the data and process / analyze and present it to the customer as needed. However the data traffic will incase to this client as the number of real data publishers increases. use MQTT5 shared subscription to avoid data overloading.
The MQTT gateway is usually a piece of software/hardware between sensors and the IoT operating system or server. It receives data from edge sensors and converts them into messages and passes them to IoT applications. The part of the server is the gateway. The gateway acts as a middleware agent between the sensors and the server. This gateway has a built-in function to detect all available sensors on the network running around the gateway. The detected sensor and device are allowed to communicate with each other and are managed from the central IoT platform. The self-advertising of sensors and devices helps to find the Gateway to the Clients.
Looking to learn more about the Protocol to building IoT Application and Clients