MQTT is a Client Server Publish/Subscribe messaging transport protocol. It is open source, easy to design and light weight. This protocol can be used in constraint environment where small code is required and bandwidth is at minimum level. This protocol works on pub/sub concept which is an alternative to old client server protocol, where client and server communicates directly with each other. In MQTT protocol, publisher is particular called client who sends the message and the recipient is called subscriber. Pub/Sub don’t know the existence of each other, there is an intermediate called broker which is connected by both publisher and subscriber. The broker is used to filter the messages and distribute them accordingly.

Depending on what you want to achieve, MQTT decouples the space which means they just have to know the hostname/IP add and port of broker to pub/sub the messages. MQTT is also able to store messages during the offline state of client (Quality of Service should be greater than 0).

How to establish a connection?

A MQTT (can be publisher as well as subscriber) client is any device connected with a server, that has a MQTT library running and it is connected with MQTT broker. MQTT broker is the main heart of the complete protocol. It is responsible for receiving the messages, filtering it based on who is interested and sending the message to subscribed clients. Since, all the messages passes through the broker, it is also responsible for authentication and authorization and it can be easily integrated into backend systems. As it is exposed to internet, it handles lot of client and therefor it is highly scalable, integratable and easy to monitor. MQTT works on TCP/IP stack. The connection is established by sending a CONNECT message to the broker. The broker response with a CONNACK. When the client initiates the CONNECT message the following content will be send. clientId, cleanSession, username, password and other additional information is sent. As suggest, clientId is an identifier and has to be unique per broker. Broker response with CONNACK message, it indicates that the connection was successful and it there is any issue it also mentions it using a returnCode.

How to communicate?

After the connection is established successfully, the client can publish messages. When the client publishes, it should have a topic, which will be used by the broker to forward it to the subscribed clients. It should have Qos, payload (which contains the actual message in byte format).  Qos determined the guarantee of message reaching to the other end. It is possible to send text and images using any encoding and encryption in binary format. A published message has to be subscribed by anyone to make it useful and receive relevant messages. This packet contains unique identifier and a list of subscriptions. Each subscription is confirmed by the broker by sending acknowledgment to the client by SUBBACK message. You can unsubscribe to a particular message with UNSUBSCRIBE message.