What is MQTT Protocol

What you will learn here about MQTT Protocol:

  • What is MQTT Protocol?
  • Where you can use MQTT protocol
  • MQTT related terms
  • What do you mean by message queue telemetry transport protocol?
  • MQTT clients

What is MQTT Protocol?

MQTT protocol 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.

Where  you can use MQTT Protocol:

Below the applications of MQTT Protocol are given.
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 data 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.

5)To make a real time web application. Please click on the below link to know, how to make real time web application using MQTT.

How to make real time web application

MQTT protocol Involves following terms:

1)Clients:
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.

In MQTT Protocol, Publishers and Subscribers are independent of each other what it mean is publisher doesn’t know who is Subscriber and Subscriber doesn’t know who is Publisher. The Job of Publisher is to publish or send data only to Broker and The job of Subscriber is subscribe or receive data only from Broker.

2)MQTT broker:
MQTT broker is a 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.

3)topic(-t)
The topic is like a Communication channel on which Publisher publishes data and Subscriber subscribes data.

4)Host Name(-h)
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.

5)Port(-p)
Port is used to identify a service. Default port for MQTT is 1883.

6)Keepalive(-k)
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.

7)User name(-u)
User name required when authentication and authorization is enabled on server or broker.

8)password(-P)
password required when authentication and authorization are enabled on the server or broker.

Mosquitto username password on Windows

MQTT Username Password on linux or raspberry pi

9)Qos(-q)
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.

3)Qos2-Exactly once
In Qos2 the sender and receiver engage in a two-level handshake to ensure only one copy of the message is received (assured delivery).

10)Payload
The payload is nothing but actual the data which is to be sent from one place to another place. The broker can receive the maximum payload size of 268435455 bytes.

What do mean by Message Queue Telemetry Transport protocol?

Message Queue Telemetry transport means if a broker receives a topic for which there are no current subscribers, it will discard the topic unless the publisher indicates that the topic is to be retained. This allows new subscribers to a topic to receive the most current value rather than waiting for the next update from a publisher.

How many clients can subscribe MQTT broker or mosquitto simultaneously?

Default 1024 subscribers can subscribe to MQTT broker simultaneously. If  you want to limit number of subscribers connection, please uncomment max_connections -1 in mosquitto.conf file of MQTT broker (please check mosquitto installed folder).

Example: To limit 10 connection at time modify as follows
max_connections 10

Can  we use MQTT broker to implement real time web Application?

In order to implement a real time web application using MQTT then MQTT WebSocket is the only way.  If you do not know, how to build real time web application using MQTT websocket then please click on the below link.

How to make real time web application

MQTT CLIENT

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.

ESP8266 AS MQTT PUBLISHER AND SUBSCRIBER

PYTHON AS MQTT PUBLISHER AND SUBSCRIBER

JAVA AS MQTT PUBLISHER AND SUBSCRIBER

WINDOWS COMMAND PROMPT AS MQTT PUBLISHER AND SUBSCRIBER

Please click on the below link to become master in MQTT.

Master MQTT Protocol