MQTT
2 min read
MQTT is an OASIS standard messaging protocol for the Internet of Things (IoT). It is designed as an extremely lightweight publish/subscribe messaging transport that is ideal for connecting remote devices with a small code footprint and minimal network bandwidth. MQTT today is used in a wide variety of industries, such as automotive, manufacturing, telecommunications, oil and gas, etc.

Concepts
1. Publisher-Subscriber Model:
MQTT follows a publish-subscribe communication model, where clients (publishers) send messages to topics, and other clients (subscribers) receive messages from those topics.
2. Broker:
The MQTT broker is a server that receives published messages from clients and delivers them to the appropriate subscribers.
3. Topics:
Topics act as channels or categories to which messages are published. Subscribers express interest in specific topics to receive relevant messages.

MQTT Components
1. Publisher:
• A client that sends messages to a specific topic.
2. Subscriber:
• A client that expresses interest in a specific topic and receives messages published to that topic.
3. Broker:
• The server that handles message routing between publishers and subscribers.

Topics and Subscriptions
Each message is inside a topic allowing for organizing them.
1. Topics:
• Hierarchical and structured, topics enable flexible message routing.
2. Wildcards:
• MQTT supports wildcards (+ and #) for subscribing to multiple topics.

Real-world Use Case
1. IoT (Internet of Things):
• MQTT is widely used in IoT scenarios for efficient communication between devices.
2. Home Automation:
• Smart homes leverage MQTT for controlling and monitoring devices. For example zigbee2mqtt with Home Assistant.
3. Messaging in Remote Locations:
• Suitable for scenarios with low bandwidth, high latency, or unreliable connections.

Docker Compose
To use MQTT with mosquitto as a broker:
yaml
version: "3"
mqtt:
image: eclipse-mosquitto:2.0
restart: unless-stopped
volumes:
- "./mosquitto:/mosquitto"
ports:
- "1883:1883"