Overview
NATS Server provides native MQTT protocol support, allowing MQTT clients to connect and communicate seamlessly with NATS. This bridging enables MQTT devices to leverage NATS’ performance and scalability while maintaining standard MQTT compatibility.NATS supports MQTT 3.1.1 protocol specification with Quality of Service (QoS) levels 0, 1, and 2.
Why MQTT on NATS?
Integrating MQTT with NATS provides unique advantages:- Protocol Bridging: MQTT clients communicate directly with NATS clients
- Unified Infrastructure: Single server for both MQTT and NATS protocols
- JetStream Persistence: MQTT messages leverage JetStream for reliability
- Scalability: NATS clustering and gateway capabilities for MQTT
- IoT Ready: Native support for resource-constrained devices
- No External Broker: Built directly into NATS Server
MQTT Protocol Support
Protocol Version
NATS implements MQTT v3.1.1 (protocol level 4) as specified in the OASIS MQTT specification.Protocol Constants
Quality of Service Levels
MQTT QoS levels determine message delivery guarantees:QoS 0 - At Most Once
QoS 0 - At Most Once
Fire and forget delivery with no acknowledgments.
- Single delivery attempt
- No persistence or retries
- Lowest overhead
- Best for telemetry and non-critical data
QoS 1 - At Least Once
QoS 1 - At Least Once
Acknowledged delivery with possible duplicates.
- Message persisted to JetStream
- Requires PUBACK acknowledgment
- Redelivered if not acknowledged
- Default acknowledgment wait: 30 seconds
QoS 2 - Exactly Once
QoS 2 - Exactly Once
Four-way handshake for exactly-once semantics.
- Message deduplication with JetStream
- PUBREC → PUBREL → PUBCOMP flow
- Highest reliability, highest overhead
- Ideal for critical commands
MQTT Packets
NATS implements all standard MQTT packet types (mqtt.go:42-56):MQTT Packets
MQTT to NATS Bridging
Subject Translation
MQTT topics are automatically translated to NATS subjects:Topic Mapping
- MQTT
+(single-level) → NATS* - MQTT
#(multi-level) → NATS> - MQTT
/(separator) → NATS.
The
# wildcard in MQTT matches the current level AND all sub-levels. NATS creates two subscriptions: one for the exact subject and one with > wildcard (see mqtt.go:104-105).Message Flow
MQTT and NATS clients can communicate seamlessly:JetStream Integration
MQTT uses JetStream for QoS 1 and QoS 2 persistence (see README-MQTT.md:125-177): Streams Created:Configuration
Basic MQTT Setup
Enable MQTT by configuring the MQTT port:nats.conf
Advanced Configuration
mqtt-advanced.conf
MQTT listener port
How long to wait for QoS acknowledgments before redelivery (mqtt.go:145)
Maximum outstanding unacknowledged QoS 1 messages per session (mqtt.go:149)
MQTT over WebSocket
MQTT clients can connect via WebSocket:mqtt-websocket.conf
ws://localhost:8080/mqtt (mqtt.go:191)
Use Cases
IoT Device Integration
MQTT Device Example
Hybrid MQTT/NATS Applications
NATS Client Bridge
Retained Messages
MQTT retained messages persist the last value:Retained Messages
$MQTT_rmsgs JetStream stream.
Last Will and Testament
Configure automatic notification on disconnect:Last Will
Session Management
MQTT sessions track subscriptions and unacknowledged messages:Clean Sessions
Session Types
$MQTT_sess JetStream stream and survive server restarts.
Client ID Requirements
Every MQTT client must have a unique client ID:- Used as session identifier
- Hashed for internal storage (mqtt.go:60-63)
- Duplicate IDs cause previous connection to disconnect
- Protection against connection flapping (mqtt.go:179-182)
Monitoring
Monitor MQTT connections via/connz endpoint:
Monitor MQTT Connections
MQTT Streams
Implementation Details
From the MQTT implementation documentation (README-MQTT.md):- Connection Handling: MQTT connections create a NATS client with
mqttfield set - Packet Processing: Custom
mqttParse()processes MQTT wire protocol - Account Isolation: MQTT streams created per-account with session manager
- Stream Replication: MQTT streams support clustering with configurable replicas
- Subject Mapping: Automatic translation between MQTT topics and NATS subjects
Performance Considerations
QoS 0 for Throughput
Use QoS 0 for high-volume telemetry where occasional message loss is acceptable.
Tune Ack Wait
Adjust
ack_wait based on client connectivity. Longer for unreliable networks.Session Limits
Persistent sessions consume memory. Use clean sessions for transient clients.
Max Ack Pending
Increase
max_ack_pending for high-throughput QoS 1/2 subscriptions.Limitations
Next Steps
JetStream
Learn about JetStream persistence used by MQTT
WebSocket
Connect MQTT clients over WebSocket
Configuration
Detailed MQTT configuration options
Monitoring
Monitor MQTT connections and streams