MockForge MQTT
MQTT protocol support for MockForge with full broker simulation, topic management, and QoS handling.
This crate provides comprehensive MQTT mocking capabilities for IoT applications, pub/sub systems, and message queue testing. Perfect for testing MQTT clients, brokers, and IoT device communication without requiring external MQTT infrastructure.
Features
- Full MQTT Broker: Complete MQTT 3.1.1 and 5.0 protocol support
- Topic Management: Hierarchical topic structure with wildcards
- QoS Levels: Support for QoS 0, 1, and 2 message delivery
- Session Management: Persistent sessions and clean session handling
- Retained Messages: Store and deliver retained messages
- Will Messages: Last will and testament message handling
- Authentication: Configurable client authentication
- Metrics & Monitoring: Comprehensive MQTT metrics collection
- Fixture System: YAML-based message templates and auto-publishing
Quick Start
Basic MQTT Broker
use ;
async
Testing with MQTT Clients
use ;
use Duration;
async
Core Components
MqttBroker
The main broker implementation handling all MQTT protocol operations:
use ;
let config = MqttConfig ;
let spec_registry = new;
let broker = new;
Topic Management
Hierarchical topic structure with wildcard support:
use TopicTree;
// Create topic tree
let topic_tree = new;
// Topics support wildcards:
// + (single level) and # (multi-level)
topic_tree.subscribe;
topic_tree.subscribe;
topic_tree.subscribe;
QoS Handling
Support for all MQTT Quality of Service levels:
use ;
// QoS 0: At most once (fire and forget)
let qos_0 = publish_at_most_once;
// QoS 1: At least once (acknowledged delivery)
let qos_1 = publish_at_least_once.await?;
// QoS 2: Exactly once (two-phase commit)
let qos_2 = publish_exactly_once.await?;
Session Management
Persistent sessions for reliable messaging:
use ClientSession;
// Clean session (default)
let clean_session = ClientSession ;
// Persistent session
let persistent_session = ClientSession ;
Fixture System
Define message templates and auto-publishing rules using YAML:
# mqtt-fixture.yaml
topics:
- name: "sensors/temperature"
retained: false
- name: "devices/status"
retained: true
fixtures:
- topic: "sensors/temperature"
payload: '{"sensor_id": "temp-001", "value": 23.5, "unit": "celsius"}'
qos: 1
retain: false
- topic: "devices/status"
payload: '{"device_id": "dev-001", "status": "online", "battery": 85}'
qos: 0
retain: true
auto_publish:
- topic: "sensors/temperature"
payload_template: '{"sensor_id": "temp-{{sensor_id}}", "value": {{temperature}}, "timestamp": "{{now}}"}'
qos: 1
interval_seconds: 30
duration_seconds: 300
variables:
sensor_id: "001"
temperature: "22.5"
- topic: "iot/heartbeat"
payload_template: '{"service": "{{service_name}}", "status": "alive", "uptime": {{uptime}}}'
qos: 0
interval_seconds: 60
variables:
service_name: "mockforge-mqtt"
uptime: 3600
Loading Fixtures
use ;
// Create broker with fixture support
let spec_registry = new;
let broker = new;
// Load fixtures from file
broker.load_fixtures_from_file.await?;
// Or create fixtures programmatically
use ;
let fixture = MqttFixture ;
broker.add_fixture.await?;
Supported MQTT Features
Protocol Versions
- MQTT 3.1.1: Legacy protocol support
- MQTT 5.0: Latest protocol with enhanced features
Message Types
- CONNECT: Client connection establishment
- CONNACK: Connection acknowledgment
- PUBLISH: Message publication
- PUBACK/PUBREC/PUBREL/PUBCOMP: QoS flow control
- SUBSCRIBE: Topic subscription
- SUBACK: Subscription acknowledgment
- UNSUBSCRIBE: Topic unsubscription
- UNSUBACK: Unsubscription acknowledgment
- PINGREQ/PINGRESP: Keep-alive handling
- DISCONNECT: Clean disconnection
Advanced Features
- Will Messages: Last will and testament
- Retained Messages: Persistent topic messages
- Topic Aliases: Bandwidth optimization (MQTT 5.0)
- Subscription Identifiers: Subscription tracking (MQTT 5.0)
- User Properties: Custom metadata (MQTT 5.0)
Configuration
MqttConfig
use ;
let config = MqttConfig ;
Environment Variables
# Server configuration
# Connection limits
# Protocol settings
Testing Examples
Publisher Testing
use ;
use Duration;
async
Subscriber Testing
use ;
use StreamExt;
async
QoS Testing
use ;
async
Retained Messages
use ;
async
Performance
MockForge MQTT is optimized for testing scenarios:
- In-Memory Operations: Fast message routing without persistence
- Concurrent Connections: Handle multiple simultaneous MQTT clients
- Low Latency: Minimal overhead for message operations
- Scalable: Support for high-throughput IoT testing scenarios
- Resource Efficient: Configurable connection limits and cleanup
Integration with MockForge
MockForge MQTT integrates seamlessly with the MockForge ecosystem:
- MockForge Core: Shared configuration and logging
- MockForge CLI: Command-line MQTT broker management
- MockForge Data: Enhanced message generation with templates
- MockForge Observability: Metrics and tracing integration
Troubleshooting
Common Issues
Connection refused:
- Ensure broker is started and listening on correct port
- Check firewall settings and port availability
- Verify client connection parameters
Messages not received:
- Check topic subscription patterns and wildcards
- Verify QoS levels match between publisher and subscriber
- Check retained message settings
QoS issues:
- Ensure broker supports requested QoS level
- Check network reliability for higher QoS levels
- Verify client acknowledgment handling
Session persistence:
- Check clean session flag settings
- Verify client ID consistency across connections
- Check session expiry settings
Examples
See the examples directory for complete working examples including:
- Basic MQTT broker setup
- Publisher/subscriber testing patterns
- QoS level verification
- Retained message scenarios
- IoT device simulation
- Load testing with multiple clients
Related Crates
mockforge-core: Core mocking functionalityrumqttc: MQTT client library for testingrumqttd: Underlying MQTT broker implementation
License
Licensed under MIT OR Apache-2.0