knowledge/technology/internet/MQTT.md

52 lines
2.3 KiB
Markdown
Raw Normal View History

2023-12-04 10:02:23 +00:00
---
obj: concept
wiki: https://en.wikipedia.org/wiki/MQTT
website: https://mqtt.org/
---
# MQTT
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:**
2023-12-18 23:21:12 +00:00
- Smart homes leverage MQTT for controlling and monitoring devices. For example [zigbee2mqtt](../applications/web/zigbee2mqtt.md) with [Home Assistant](../applications/web/Home%20Assistant.md).
2023-12-04 10:02:23 +00:00
### 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"
```