Message Queuing Telemetry Transport (MQTT) is a popular and lightweight publish-subscribe messaging protocol that is ideal for connecting Internet of Things (IoT) or machine-to-machine (M2M) devices and applications over the internet. MQTT is designed to operate efficiently in low-bandwidth or low-power environments, making it an ideal choice for applications with a large number of remote clients. It’s used in a variety of industries, including consumer electronics, automotive, transportation, manufacturing, and healthcare.

How Does MQTT Work?

Devices or applications connected via MQTT are known as clients. These clients publish and/or subscribe to messages on a specific topic or on multiple topics. Subscribed clients receive all messages published to that topic, enabling efficient and fault-tolerant data exchange between many devices and services.

At the heart of an MQTT architecture is a broker. The broker is a server responsible for tracking clients (and any topics they’re subscribed to), processing messages, and routing those messages to appropriate systems.

MQTT Protocol Versions

Several key versions of the MQTT protocol have been adopted as a standard. The OASIS organization manages revisions of the protocol and maintains a complete specification for each version. Each revision of MQTT has expanded on the protocol feature set, so it’s important to know which version of MQTT your devices and broker adhere to.

MQTT Message Types and Format

There are several types of MQTT messages, and each message type contains a specific format that conforming devices and applications must follow. Revisions to the MQTT protocol vary in format and message types. A complete list of message types can be found in each specification of the MQTT protocol.

The three most common types of messages are CONNECT, PUBLISH, and SUBSCRIBE. Each MQTT message contains a header, payload, and optional flags. Depending on the message type, the payload can be variable in length. For example, in a PUBLISH message, the payload contains the data to be sent to all subscribing devices and the length of the data field has a direct bearing on the message’s size.  By default, MQTT devices connect on port 1883 in an unsecured fashion or port 8883 when SSL/TLS encryption has been enabled.

What Is an Example of MQTT in Action?

Smart cars are a great example of MQTT in action. As auto manufacturers add new features to support everything from remote diagnostics and fleet management to fuel payments and entertainment, MQTT has become a common standard for connected cars. Unlike the Hypertext Transfer Protocol (HTTP) protocol, MQTT can maintain a persistent session even when a car moves in and out of dead zones or if its connection changes cell towers. MQTT also supports bidirectional communications, making it easier for the car and cloud application to send and receive data without waiting for the other to respond.

What Is the Difference Between HTTP and MQTT?

HTTP and MQTT are both network protocols used to transmit data over the internet. Let’s look at their differences.

HTTP

  • Request-response protocol where a client sends a request to a server and the server responds with the requested data.
  • Primarily designed for transmitting web content (such as HTML documents, images, and form data) between web servers and browsers.
  • Example: A web browser (the client) sends a request to a web server, and the server responds with data in the form of a webpage.

MQTT

  • Publish-subscribe, lightweight messaging protocol where clients subscribe to topics and receive messages published to those topics by other clients.
  • Designed for network topologies and devices where low bandwidth, connection stability, and power consumption are important considerations.
  • Example: The smart car scenario above.
Why Use MQTT in the Internet of Things (IoT)?

Many features of MQTT make it the ideal protocol for messaging between IoT devices (the “things” in IoT) and backend systems. Here, we focus on four features:

  • Lightweight – MQTT has a small code footprint that works well in devices with limited processing power and memory, such as sensors.
  • Reliable – Many IoT devices connect over cellular networks. MQTT is a protocol suited to low bandwidth networks that require compact messages using less data. This makes MQTT more reliable, even when network bandwidth is limited or unstable.
  • Scalable – The publish-subscribe model is easy to scale up with the addition of devices and backend systems. One example of a single device publishing to two separate backend networks (subscribers) is a residential smart meter that sends utility usage data to both the utility’s systems for billing purposes and to a customer-facing application that the homeowner can access for keeping tabs on their residence’s energy usage.
  • Secure – MQTT messages can be encrypted with standard Transport Layer Security (TLS) and support credentials that can be used for authentication. This makes MQTT a secure messaging protocol in IoT applications that can handle sensitive information like health monitor readings from various medical devices.
What Transport Protocol Does MQTT Use?

MQTT supports Transmission Control Protocol/Internet Protocol (TCP/IP) as its underlying transport protocol. This widely used networking protocol ensures messages are sent reliably between clients and brokers.

There are several reasons why TCP/IP is considered reliable and efficient:

  • Error detection and correction – A variety of techniques verify the integrity of data packets and retransmission mechanisms to recover lost packets.
  • Flow control – Data is transmitted at optimal rates for any given network, preventing delays in transmission and promoting efficient communication.
  • Multiplexing – Multiple streams of data can be sent over a single connection, so multiple applications can use the same connection at the same time.
  • Compatibility – Works with a wide variety of devices and operating systems.
  • Scalability – Designed to work in large, complex networks without compromising performance even when handling large amounts of traffic.

While it’s the most common, TCP/IP is not the only choice for transport of MQTT messages. The MQTT protocol also works over User Datagram Protocol (UDP) and WebSockets.

Additional Resources

We are proud to offer the following free resources to help you continue researching how NGINX can secure, load balance, and provide high availability for MQTT-based IoT systems.

Blog

Docs