MQTT Explained for Cloud & DevOps Engineers

Listen to this Post

MQTT (Message Queuing Telemetry Transport) is a lightweight publish-subscribe (pub/sub) protocol designed for efficient communication between systems, particularly in IoT, real-time data, and low-bandwidth environments.

MQTT Workflow

1. 🔐 Connect & Authenticate

  • Publisher connects to the MQTT broker (Port `1883` for unencrypted, `8883` for TLS).
  • Authentication via username/password or certificates.

2. 📤 Publish Message

  • Publisher sends a message with a QoS level (0, 1, or 2) and an optional Retain flag.

3. 🧭 Route via Topic Tree

  • Broker routes messages using topics (e.g., sensor/+/temperature).

4. 📬 Deliver to Subscribers

  • Subscribers receive messages based on topic matching and QoS.

5. 📥 Subscribe to Topics

  • Subscribers subscribe to topics to receive relevant messages.

You Should Know: MQTT Practical Implementation

1. Installing MQTT Broker (Mosquitto)

 On Ubuntu/Debian 
sudo apt update 
sudo apt install mosquitto mosquitto-clients

Start & Enable Mosquitto 
sudo systemctl start mosquitto 
sudo systemctl enable mosquitto

Check status 
sudo systemctl status mosquitto 

2. Basic MQTT Commands

Publish a Message:

mosquitto_pub -h localhost -t "test/topic" -m "Hello MQTT" -p 1883 

Subscribe to a Topic:

mosquitto_sub -h localhost -t "test/topic" -p 1883 

3. Python MQTT Example (Using Paho-MQTT)

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc): 
print("Connected with result code " + str(rc)) 
client.subscribe("test/topic")

def on_message(client, userdata, msg): 
print(f"Received: {msg.topic} {msg.payload.decode()}")

client = mqtt.Client() 
client.on_connect = on_connect 
client.on_message = on_message

client.connect("localhost", 1883, 60) 
client.loop_forever() 

4. Securing MQTT with TLS

 Generate Certificates 
openssl req -new -x509 -days 365 -nodes -out mosquitto.crt -keyout mosquitto.key

Configure Mosquitto for TLS 
echo "listener 8883 
cafile /etc/mosquitto/certs/mosquitto.crt 
keyfile /etc/mosquitto/certs/mosquitto.key 
certfile /etc/mosquitto/certs/mosquitto.crt" | sudo tee -a /etc/mosquitto/conf.d/ssl.conf

Restart Mosquitto 
sudo systemctl restart mosquitto 

5. MQTT QoS Levels

  • QoS 0: At most once (Fire and forget)
  • QoS 1: At least once (Guaranteed delivery)
  • QoS 2: Exactly once (No duplicates)

What Undercode Say

MQTT is a powerful protocol for IoT, DevOps, and real-time messaging. Mastering it involves:
– Understanding topic hierarchy for efficient routing.
– Securing brokers with TLS & authentication.
– Using QoS levels appropriately for reliability.
– Integrating with Python, Node.js, or Go for automation.

Additional Useful Commands

 Check active MQTT connections 
sudo netstat -tulnp | grep mosquitto

Enable MQTT logging 
sudo tail -f /var/log/mosquitto/mosquitto.log

Test MQTT with Docker 
docker run -it --rm -p 1883:1883 eclipse-mosquitto 

Expected Output:

Connected with result code 0 
Received: test/topic Hello MQTT 

🔗 Further Reading:

References:

Reported By: Sandip Das – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image