CodeBlog.xyz

MQTT – Message Queuing Telemetry Transport

September 1, 2023 | by Meir Achildiev

MQTT – Message Queuing Telemetry Transport

What is MQTT?

MQTT stands for Message Queuing Telemetry Transport. It’s a lightweight publish/subscribe messaging protocol designed for low-bandwidth, high-latency, or unreliable networks. MQTT has found its way into various kinds of systems like IoT devices, home automation, and even mobile apps.

When and by whom it was developed:

MQTT was originally developed by Andy Stanford-Clark of IBM and Arlen Nipper of Arcom (now Cirrus Link) in 1999 to connect oil pipelines over satellite links.

First Usage:

The protocol’s first use case was for the monitoring of an oil pipeline through the desert to enable reliable communications over problematic networks.

Pros and Cons:

Pros:

  • Low Bandwidth: Efficient packet size is perfect for conditions where network bandwidth is limited.
  • Quality of Service Levels: Offers three levels of QoS for message delivery:
    1. At most once (0)
    2. At least once (1)
    3. Exactly once (2)
  • Last Will and Testament: Allows for informing other clients about a sudden disconnect.
  • Retained Messages: Stores the last relevant message for a topic for a while.

Cons:

  • Not HTTP-based: MQTT isn’t web-friendly like REST.
  • Broker Required: Requires a centralized broker to manage publications and subscriptions.
  • Limited Security: The standard doesn’t define fine-grained security mechanisms (though SSL/TLS is often used).

When to Use It:

  • IoT Devices: Resource-constrained devices like sensors.
  • Real-time Analytics: Stock market dashboards or live sports updates.
  • Instant Messaging: Lightweight real-time chat apps.

When Not to Use It:

  • Stateful Applications: Where the previous state matters for the next transaction.
  • For Complex Queries: Like in databases where joins, updates, and deletes are frequently required.
  • Heavy Text-based Operations: MQTT isn’t designed for text-based protocols like JSON or XML parsing.

Most Recommended Scenario:

The most recommended scenario for using MQTT is in IoT applications where bandwidth is constrained and you need real-time updates.

Code Example in C# and .NET Core

First, you’ll need to install the M2Mqtt package, a .NET library for MQTT.

dotnet add package M2Mqtt --version 4.3.0

Publisher Example:

Here’s how you would publish a message on a topic.

using System;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

class Program {
    static void Main(string[] args) {
        MqttClient client = new MqttClient("broker_address");
        string clientId = Guid.NewGuid().ToString();
        client.Connect(clientId);
        
        string topic = "home/livingroom/temperature";
        string message = "22.5";
        
        client.Publish(topic, System.Text.Encoding.UTF8.GetBytes(message), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
    }
}

Subscriber Example:

Here’s how you would subscribe to a topic.

using System;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

class Program {
    static void Main(string[] args) {
        MqttClient client = new MqttClient("broker_address");
        
        client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
        
        string clientId = Guid.NewGuid().ToString();
        client.Connect(clientId);
        
        client.Subscribe(new string[] { "home/livingroom/temperature" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
    }

    private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) {
        string receivedMessage = System.Text.Encoding.UTF8.GetString(e.Message);
        Console.WriteLine("Received message: " + receivedMessage);
    }
}

Additional Important Information:

  • MQTT brokers like Mosquitto, HiveMQ, or cloud-based options like AWS IoT Core and Azure IoT Hub are used for production systems.
  • MQTT over Websockets: MQTT can also be run over websockets which makes it web-friendly.

RELATED POSTS

View all

view all