MQTT and AMQP are both messaging protocols used in IoT, but they solve different problems with fundamentally different approaches.
Let’s see how they actually work. Imagine you have a temperature sensor on a remote weather station.
With MQTT, the sensor (a client) publishes a message like {"temperature": 25.5} to a topic /weather/station01/temperature. An MQTT broker, acting as a central hub, receives this message. Any client subscribed to that topic, say a dashboard application, will then receive the message from the broker. The sensor doesn’t know or care who is listening.
// Sensor publishes to broker
{"topic": "/weather/station01/temperature", "payload": "{\"temperature\": 25.5}"}
// Broker forwards to subscribed dashboard
{"topic": "/weather/station01/temperature", "payload": "{\"temperature\": 25.5}"}
AMQP, on the other hand, is more like a postal service with strict delivery rules. A sender (an application) sends a message to an exchange. The exchange, based on routing rules, forwards the message to one or more queues. Consumers then pull messages from these queues. It’s a more structured, explicit flow.
// Application sends message to exchange
{
"exchange": "weather_updates",
"routing_key": "station01.temperature",
"payload": {"temperature": 25.5}
}
// Exchange routes to queue
// Queue: "temperature_readings_for_dashboard"
// Dashboard consumes from queue
{
"payload": {"temperature": 25.5},
"metadata": {"routing_key": "station01.temperature", ...}
}
The core problem MQTT solves is efficient, low-overhead communication for devices with limited bandwidth and processing power, often in unreliable network conditions. It’s designed for publisher-subscriber patterns where many devices might send data to many consumers without direct knowledge of each other. The broker handles the fan-out and persistence.
AMQP, conversely, is built for enterprise-level messaging, focusing on reliable delivery, transactional integrity, and complex routing. It’s ideal for scenarios where message order, guaranteed delivery, and integration between diverse systems are paramount. AMQP brokers offer more control over message flow, acknowledgments, and delivery guarantees.
Here’s a typical MQTT configuration snippet for a broker (Mosquitto):
# mosquitto.conf
listener 1883
protocol mqtt
allow_anonymous false
password_file /etc/mosquitto/passwd
And a corresponding client connection in Python using Paho MQTT:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt.Client("python_publisher")
client.username_pw_set("user", "password")
client.on_connect = on_connect
client.connect("mqtt.example.com", 1883)
client.loop_start()
client.publish("/sensors/temp", '{"value": 22.1}')
client.loop_stop()
For AMQP, consider RabbitMQ. A simplified client interaction might look like this (using Python’s pika library):
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq.example.com'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', exchange_type='direct')
channel.basic_publish(
exchange='logs',
routing_key='info',
body='{"message": "System started successfully."}'
)
print(" [x] Sent 'Hello World!'")
connection.close()
The fundamental difference lies in their architectural design: MQTT is a lightweight publish-subscribe protocol, while AMQP is a more robust, feature-rich message queuing protocol. MQTT brokers are simpler, primarily responsible for message routing based on topics. AMQP brokers (like RabbitMQ) are more complex, managing exchanges, queues, bindings, and sophisticated delivery semantics.
A common misconception is that MQTT is inherently "less reliable" than AMQP. While MQTT has different Quality of Service (QoS) levels (0, 1, 2) that offer varying degrees of delivery assurance, its primary design goal is efficiency. AMQP, with its built-in acknowledgments, transactions, and durable queues, is engineered for scenarios where guaranteed delivery and message persistence are non-negotiable, even at the cost of higher overhead. For instance, a critical financial transaction might use AMQP, whereas a stream of sensor readings where occasional loss is acceptable would favor MQTT.
The next step is understanding how to secure these protocols.