Introduction to MQTT protocol for IOT




Internet of Things is very popular now a day. In simple words, Internet of things means that connecting physical devices over the internet. The basic idea of IoT (internet of things) is to connect physical devices over the internet in such a way that they can collect or exchange data with each other and these devices can be operated by any person. But one thing we should keep in mind that for exchanging and collecting data, the device from which data is to be send or receive should be in online mode i.e. connected to Internet. Examples of some physical devices are refrigerator, smart phones or any other electronic device. In last tutorial, we have seen different type of IOT protocols.




Increased number of affordable microcontrollers like Arduino and Raspberry Pi to name a few are used to enable cheap devices for measuring sensor data and then send it over Internet. In order to make these devices communicate with each other, we need some language in which they can communicate and understand each other. So the language which is used to make these devices communicate with each other is known as PROTOCOL. A large number of protocols have been introduced so far and many other protocols are continuously being introduced.

Introduction to MQTT protocol for IOT

There are a large number of protocols which are used in devices so that they can exchange or collect information from each other over the Internet. In this article we are going to introduce you to MQTT protocol. MQTT (MQ telemetry transport) previously known as Message Queuing Telemetry Transport protocol is a light weight protocol and it is used to provide resource constrained network clients a simple way in order to distribute telemetry information. This protocol make use of subscribe communication pattern. Email address, phone number etc. are not required by MQTT clients. So just like with most messaging systems we don’t have to assign addresses to MQTT clients. Machine to machine communication (M2M) uses this publish/subscribe communication pattern and this pattern is widely used in IoT. MQTT protocol runs on top of TCP / IP in order to provide reliable data delivery.MQTT protocol




MAIN COMPONENTS OF MQTT PROTOCOL

MQTT protocol has three main components

  • Subscriber
  • Publisher
  • Broker

MQTT protocol enables resource constraint IoT devices to publish or send information about some topic to server that functions as MQTT message broker. The function of broker is to push that piece of information to only those clients that are previously subscribed to that topic. Or in simple words we can explain this basic idea as the publisher is responsible for generating and transmission of information to subscribers through broker. The main function of broker is to ensure security by checking authorization of subscribers and publishers.

ADVANTAGES OF MQTT PROTOCOL:

Some advantages of using MQTT protocol are:

  • Good choice for wireless networks
  • It can distribute information reliably
  • It can increase the scalability of network
  • It can reduce bandwidth consumption of network
  • It is well suited for control and remote sensing
  • Light weight overhead
  • Reduce updating rates to seconds
  • Uses permission based security making it more secure
  • Saves development time
  • Publish / subscribe protocol are able to collect more data by using less bandwidth as compared to other polling protocols.
  • Maximize bandwidth which is available

WORKING OF MQTT PROTOCOL

To explain working of MQTT protocol we will divide the MQTT session in 4 stages named as connection, authentication, communication and termination. So first of all a TCP / IP connection is initiated from client to broker by using a standard or custom port which is defined by broker’s operation. While establishing a connection, it’s important to recognize that whether the server has continued an old session or a new session. Old session in continued if reused client identity is provided to broker.MQTT working

There are two types of communication named as non-encrypted communication and encrypted communication. Different port numbers are assigned for both type of communication. If we talk about standard port for non-encrypted communications then port numbers are 1883 and if we need encrypted communication by using standard ports then port numbers are 8883 and they use SSL / TLS. For SSL / TLS hand shaking process id required and during this process the client authenticates the server it is going to use by validating server certificate. When client is going to establish a connection with broker, the client provides a client certificate to broker during connection establishment handshaking and this certificate are used by broker to authenticate the client. It is expected from brokers to support the authentication of client with the help of SSL / TLS client side certificates.

As we mentioned above that MQTT protocol is a resource constrained protocol and as it is widely used in IoT devices so SSL / TLS is not always an option so it is not desirable in some cases. But in such cases this authentication is presented as clear text username and password and the client send it to the server by attaching it in connect / connack packet sequence. In case of open brokers that are published on Internet, they can accept anonymous clients and as part of authentication the USERNAME and PASSWORD are simply left bank in handshaking process.

During communication phase, client can perform operations like publishing, subscribing, unsubscribing and ping operations. The basic function of publishing is to send binary block of data defined by the publisher. Format of content is application specific. For subscription service a packet pair named as subscribe / suback is used and for unsubscribing operation unsubscribe / unsuback packet pair is used. For ping operation, client can check its connection with broker by using pingreq / pingresp packet sequence. It keeps the client connection with broker alive. One important point to keep in mind is that there is no direct connection between the publisher and subscriber.

WHY MQTT PROTOCOL IS KNOWN AS LIGHTWEIGHT PROTOCOL

 MQTT is known as lightweight protocol as all messages of this protocol contains a small code footprint. Every message has a fixed header having a size of 2 bytes i.e. 16 bits only. An optional variable header is also present. Except these headers a message payload is also present and size of this message payload is limited to 256 MB of information and it also has a QoS (quality of service) level.

QUALITY OF SERVICE LEVEL:

In MQTT protocol, we have three different QoS levels that are used to determine that how our content / message will be managed. One thing we can say for sure is that obviously higher levels of quality of service are more reliable because they have more bandwidth and latency requirements.

  1. The first and simplest quality of service level is an unacknowledged service i.e. it use publish packet sequence which means that the publisher will send message to broker and broker will pass this message to subscriber and both this message delivery will take place only one time. Broker will not store the message for the case if packet is lost so there is no reliable delivery of messages.
  2. The second quality of service level is acknowledged service. It make use of publish / puback packet sequence for communication between publisher and broker as well as broker and subscriber. In case of packet loss, a retry mechanism is present that will send that message from its buffer space. In this quality of service a subscriber can have multiple copies of same message if the message is received but acknowledgment was not received timely.
  3. The third and last level of quality of service is known as assured service. In this quality of service it covers drawbacks of 1st and 2nd level of quality of service by delivering each message with two packet pairs. The first pair is known as publish / pubrec and second pair is known as pubrel / pubcom. The purpose of using these two pairs is to ensure that message is sent only once irrespective of the number of retries in case of packet lost.

APPLICATIONS OF MQTT PROTOCOL:

MQTT protocol is widely used and some of the main applications of this protocol are given below:

  • Used in Facebook messenger application
  • AWS
  • Google cloud
  • Microsoft Azure
  • Smart homes
  • Healthcare
  • Logistics

So in this article we discussed about MQTT protocol, its advantages, working, quality of service provided by this protocol and some applications of this protocol. Any queries related to this article can be asked in comment section below.



Leave a Comment