Demystifying MQTT: Why is it the protocol of choice for the IoT?

Insert image description here

Introduction to MQTT protocol

Overview

MQTT is a lightweight message transmission protocol based onpublish/subscribe model, specifically designed for low bandwidth and unstable network environments. Designed for IoT applications, it can provide real-time and reliable messaging services to connected devices with very little code. The MQTT protocol is widely used in the Internet of Things, mobile Internet, smart hardware, Internet of Vehicles, smart cities, telemedicine, electric power, petroleum and energy and other fields.

The MQTT protocol was developed by Andy Stanford-Clark (IBM) and Arlen Nipper (Arcom, now Cirrus Link) was released in 1999. According to Nipper's introduction, MQTT must have the following points:

  • Simple and easy to implement
  • Support QoS (the device network environment is complex)
  • Lightweight and bandwidth-saving (because bandwidth was expensive at that time)
  • Data independent (not concerned about Payload data format)
  • Continuous session awareness (always know whether the device is online)

According to Arlen Nipper’s self-report on IBM Podcast, the original name of MQTT is MQ TT, pay attention to the space between MQ and TT, its full name is: MQ Telemetry Transport, it was a crude oil pipeline data collection he participated in in the early 1990s for Conoco Phillips company A real-time data transmission protocol developed for monitoring systems (pipeline SCADA system). Its purpose is to allow sensors to communicate with IBM's MQ Integrator via bandwidth-limited VSAT . Since Nipper is a professional in remote sensing and data collection and monitoring, he chose the name MQ TT according to industry practice.

MQTT compared to other protocols

MQTT vs HTTP
  • The minimum message of MQTT is only 2 bytes, which consumes less network overhead than HTTP.
  • Both MQTT and HTTP can use TCP connections and achieve stable and reliable network connections.
  • MQTT is based on the publish-subscribe model, and HTTP is based on request-response, so MQTT supports duplex communication.
  • MQTT can push messages in real time, but HTTP requires polling for data updates.
  • MQTT is stateful, but HTTP is stateless.
  • MQTT can recover from abnormal connection disconnections, which HTTP cannot achieve.
MQTT vs XMPP

The MQTT protocol has a simple, lightweight design and flexible routing. It will completely replace the XMPP protocol in the PC era in the mobile Internet and IoT messaging fields.

  • MQTT messages are small in size and easy to encode and decode. XMPP is based on heavy XML, so the message size is large and interactions are cumbersome.
  • MQTT is based on the publish-subscribe model, which is more flexible than XMPP's JID-based point-to-point message routing.
  • MQTT supports different types of messages such as JSON and binary. XMPP uses XML to carry messages, and the binary must be Base64 encoded and processed.
  • MQTT ensures reliable transmission of messages through QoS, and the XMPP main protocol does not define a similar mechanism.

Why MQTT is the best protocol for IoT?

According to the latest "State of the Internet of Things Spring 2022" research report released by IoT Analytics, the IoT market is expected to grow by 18% by 2022, reaching 14.4 billion a>Active connection.

With such a large-scale demand for the Internet of Things, massive device access and device management have brought huge challenges to network bandwidth, communication protocols and platform service architecture. For Internet of Things protocol, several key issues in IoT device communication must be solved in a targeted manner: Network The environment is complex and unreliable, with small memory and flash capacity, and limited processor capabilities.

The MQTT protocol was created to address the above problems. After years of development, it has become the preferred protocol in the Internet of Things industry with its advantages of lightweight, efficient, reliable messaging, massive connection support, and secure two-way communication.

Insert image description here

Lightweight and efficient, saving bandwidth

MQTT minimizes the additional consumption occupied by the protocol itself. The message header only needs to occupy a minimum of 2 bytes, and can run stably in network environments with limited bandwidth. At the same time, the MQTT client only requires very small hardware resources and can run on various edge devices with limited resources.

Reliable messaging

The MQTT protocol provides three message quality of service levels (Quality of Service) to ensure the reliability of message delivery in different network environments.

  • QoS 0: Messages are delivered at most once.
    If the client is not available at the time, the message will be lost. After the publisher sends a message, it no longer cares whether it is sent to the other party, nor does it set up any resend mechanism.

  • QoS 1: Message delivered at least 1 time.
    Contains a simple resend mechanism. After the publisher sends the message, it waits for the receiver's ACK. If no ACK is received, it resends the message. This mode guarantees that the message will arrive at least once, but it cannot guarantee that the message will be repeated.

  • QoS 2: Message is delivered only once.
    Designed a retransmission and duplicate message discovery mechanism to ensure that the message reaches the other party and only arrives strictly once.

In addition to QoS, MQTT also providesClear Session (Clean Session) mechanism. For those clients who want to receive messages missed during the offline period after reconnecting, they can set up to close the clear session when connecting. At this time, the server will store the subscription relationship and offline messages for the client, and retry the session on the client. Sent to the client after going online.

Massive connection support

The MQTT protocol has taken into account the growing number of IoT devices since its birth. Thanks to its excellent design, MQTT-based IoT applications and services can be easily equipped High concurrency, high throughput, and high scalabilitycapabilities.

Connecting a large number of IoT devices is inseparable from the support of MQTT server. Currently, EMQX is the MQTT server that supports the largest number of concurrent connections. The recently released EMQX 5.0 achieved **100 million MQTT connections +** 1 million message throughput per second through a 23-node cluster, which makes EMQX 5.0 becomes the most scalable MQTT server in the world so far.

Secure two-way communication

Relying on a publish-subscribe model, MQTT allows bidirectional message communication between devices and the cloud. The advantage of the publish-subscribe model is that the publisher and the subscriber do not need to establish a direct connection, nor do they need to be online at the same time. Instead, the message server is responsible for the routing and distribution of all messages.

Security is the cornerstone of all IoT applications, and MQTT supports secure two-way communication via TLS/SSL, while the client ID, username, and password provided in the MQTT protocol allow us to implement application layer authentication and authorization.

Presence awareness

In order to cope with network instability, MQTT provides a **heartbeat keep alive (Keep Alive)** mechanism. When there is no message interaction between the client and the server for a long time, Keep Alive keeps the connection from being disconnected. If it is disconnected, the client can immediately sense it and reconnect immediately.

At the same time, MQTT designed theLast Will message to allow the server to help when it discovers that the client has gone offline abnormally. The client publishes a last wish message to the specified MQTT topic.

In addition, some MQTT servers such as EMQX also provide online and offline event notification functions. When the back-end service subscribes to a specific topic, it can receive online and offline events from all clients. This helps the back-end service uniformly handle the online and offline events of clients. event.

MQTT 5.0 and 3.1.1

Four years after MQTT 3.1.1 was released and became an OASIS standard, MQTT 5.0 is officially released. This is a major improvement and upgrade. Its purpose is not only to meet the current industry needs, but also to make adequate preparations for the future development and changes of the industry.

Based on version 3.1.1, MQTT 5.0 adds session/message delays, reason codes, topic aliases, user attributes, shared subscriptions and other features that are more in line with the needs of modern Internet of Things applications, improving the performance, stability and reliability of large-scale systems. Scalability. At present, MQTT 5.0 has become the protocol of choice for most IoT enterprises, and we recommend that developers who are new to MQTT use this version directly.

MQTT server

The MQTT server is responsible for receiving connections initiated by the client and forwarding messages sent by the client to other qualified clients. A mature MQTT server can support massive client connections and million-level message throughput, helping IoT service providers focus on business functions and quickly create a reliable MQTT application.

EMQX is a widely used large-scale distributed IoT MQTT server. Since the open source version was released on GitHub in 2013, it has been downloaded more than 10 million times worldwide and has been connected to more than 100 million key IoT devices.

Those who are interested can use the following Docker command to install the EMQX 5.0 open source version to experience it.

docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:latest

Fully managed MQTT services can also be created directly on EMQX Cloud.

MQTT client

MQTT applications usually need to be based on the MQTT client library to implement MQTT communication. At present, basically all programming languages ​​have mature open source MQTT client libraries. You can refer to the complete list of MQTT client libraries compiled by EMQ to choose a suitable one. Client library to build an MQTT client that meets your own business needs.

MQTT application development is also inseparable from the support of MQTT testing tools. An easy-to-use and powerful MQTT testing tool can help developers shorten the development cycle and create a stable IoT application.

MQTTX is an open source cross-platform desktop client. It is easy to use and provides comprehensive MQTT 5.0 function and feature testing. It can run on macOS, Linux and Windows. . At the same time, it also provides command line and browser versions to meet MQTT testing needs in different scenarios. Those who are interested can visit MQTTX official website to download and try it out.
Insert image description here
That’s all for today, everyone is welcome to download and experience it!

Reference link: https://www.emqx.com/zh/blog/what-is-the-mqtt-protocol#mqtt-%E5%8D%8F%E8%AE%AE%E7%AE%80%E4% BB%8B

Guess you like

Origin blog.csdn.net/Qingai521/article/details/134804558