MQTT Stands for message Queue Telemetry transport protocol. MQTT protocol is a machine to machine protocol(M2M) means two or more devices can directly communicate with each other using either wired or wireless communication channel. MQTT protocol is very much similar to Cable TV communication which involves Broadcasters, Setup box, and Customers. In cable TV communication different broadcasters keep sending data to their respective channel and the job of the setup box is to receive data from the broadcasters and deliver to users or customers those who have subscribed channels. Users can only see particular channels which they have subscribed. Basic cable TV communication is shown in the following figure.
In the same way MQTT communication involves Publisher, Subscriber, and Broker. The publisher is similar to Broadcaster, Broker is similar to Setup box, Subscriber is similar to User or Customer and Topic is similar to Channel. In MQTT, data published by publisher on a particular topic is received by all the subscribers who have subscribed the same topic. There can be one or multiple publisher publishing data to same or different topic simultaneously as well as there can be one or multiple subscribers subscribing data from the same or different topic simultaneously. MQTT is TCP/IP based protocol. Maximum payload or data of 268435455 bytes you can publish or send from one place to another place using MQTT . Basic MQTT protocol communication is shown in the following figure.
1)When you want to send data from one place to another place over the internet as well as local network (WiFi).
2)When you want to send between two or more devices or computers which are connected through a LAN connection.
3)When you want transmitted data should receive by one or more than one receivers simultaneously.
4)When you want transmitted data from multiple devices simultaneously should receive by single or multiple receivers simultaneously then MQTT is a good choice for you.
MQTT protocol Involves following terms:
MQTT consist of two kinds of clients communicating with a server, often called a broker. A client who sends data to broker is called as Publisher. There can be one or more publishers, publishing data to same topic(channel). A Publisher can be Java program, python script, ESP8266 NodeMCU, Command prompt of computer, linux terminal etc. A client who receives data from broker is called as Subscriber. There can be one or more subscribers, subscribing data from same topic. A Subscriber can be python script,ESP8266 NodeMCU,Command prompt of computer, linux terminal, Java program etc.
It is heart of MQTT protocol. It is responsible for receiving data from Publishers and delivers to subscribers. It is an application which you need to install on your device or computer. Broker can be install on Windows computer or Linux machine or Raspberry pi or VM instance of google Cloud etc . Installing broker on windows computer or Installing broker on LINUX or Installing broker on Raspberry PI or Installing broker on Google Cloud VM instance etc.
The topic is like a Communication channel on which Publisher publishes data and Subscriber subscribes data.
Machine or computer on which broker is installed called as host. Host name is an IP address of machine or computer on which broker is Installed or host name can be domain name corresponding to your IP address of machine on which broker is installed. A host can be Computer, Laptop, Raspberry Pi, VM instance of Google Cloud, Linux system, etc.In the case of the publisher, If Host name is localhost means Broker and Publisher are running on same machine or device. In the case of Subscriber, If Host name is localhost means Broker and Subscriber are running on the same machine or device.
Example is given below.
host name:192.168.1.101 // IP address of machine on which broker installed.
Port is used to identify a service. Default port for MQTT is 1883.
It is Duration. when the subscriber is IDLE means not receiving data from the broker, then the connection is maintained by sending PING request from a subscriber. Duration after which subscriber sending PING request to broker again and again to maintain a connection is nothing but the keep alive duration. Default keep alive Duration is 60 sec.
User name required when authentication and authorization is enabled on server to broker.
password required when authentication and authorization are enabled on the server to broker.
MQTT has 3 Quality of services(QOS) level . Default Qos is Qos0.
1)Qos0-At most Once
In Qos0 the message is sent only once and there is no check of whether data is received/delivered or not (fire and forget).
2)Qos1-at least once
In Qos1 the message is send by the sender again and again until acknowledgement is received (acknowledged delivery).Here is a chances of having duplicate message when message is received but acknowledgement is lost.
In Qos2 the sender and receiver engage in a two-level handshake to ensure only one copy of the message is received (assured delivery).
Payload is nothing but actual the data which is to be send from one place to another place. The broker can receive the maximum payload size of 268435455 bytes.
Below various mqtt clients are given by using those mqtt clients you can publish or subscribe data. mqtt clients given below such as mqtt client using ESP8266 or NODEMCU, mqtt client using python, mqtt client using java etc.