mqtt_server 1.0.0
mqtt_server: ^1.0.0 copied to clipboard
A lightweight, efficient MQTT broker implementation in pure Dart that can be embedded in applications or run as a standalone server.
MQTT Server #
A lightweight, efficient MQTT broker implementation in pure Dart. This package provides a full-featured MQTT server that can be embedded in your Dart applications or run as a standalone broker.
Features #
- MQTT 3.1.1 Protocol Support: Implements the MQTT 3.1.1 protocol specification
- QoS Levels: Supports QoS 0, 1, and 2 message delivery
- Session Persistence: Optional persistent sessions across client reconnects
- Authentication: Built-in username/password authentication system
- SSL/TLS Support: Secure connections with SSL/TLS
- Will Messages: Support for last will and testament messages
- Retained Messages: Store and forward retained messages to new subscribers
- Topic Wildcards: Support for + and # wildcards in topic subscriptions
- Clean Architecture: Modular design with clear separation of concerns
Getting Started #
Installation #
Add the package to your pubspec.yaml
:
dependencies:
mqtt_server: ^1.0.0
Then run:
dart pub get
Prerequisites #
- Dart SDK 3.0.0 or higher
Usage #
Basic Broker Setup #
import 'package:mqtt_server/mqtt_server.dart';
Future<void> main() async {
// Create a broker with default configuration
final broker = MqttBroker();
// Start the broker
await broker.start();
print('MQTT broker running on port 1883');
}
Custom Configuration #
import 'package:mqtt_server/mqtt_server.dart';
Future<void> main() async {
// Create a broker with custom configuration
final config = MqttBrokerConfig(
port: 8883,
useSSL: true,
sslCertPath: 'path/to/certificate.pem',
sslKeyPath: 'path/to/key.pem',
allowAnonymous: false,
enablePersistence: true,
persistencePath: 'mqtt_data/sessions.json',
sessionExpiryInterval: Duration(days: 1),
);
final broker = MqttBroker(config);
// Add user credentials
broker.addCredentials('username', 'password');
// Start the broker
await broker.start();
print('Secure MQTT broker running on port 8883');
}
Advanced Usage #
Session Persistence #
The broker can automatically persist sessions to disk and restore them on restart:
final config = MqttBrokerConfig(
enablePersistence: true,
persistencePath: 'data/sessions.json',
);
final broker = MqttBroker(config);
// Sessions will be saved periodically and on clean shutdown
await broker.start();
// To manually trigger session persistence
await broker.savePersistentSessions();
API Reference #
For detailed API documentation, see the API reference.
Examples #
Check the /example
directory for more comprehensive examples:
- Basic broker setup
- Secure broker with SSL/TLS
- Authentication examples
- Custom packet handling
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments #
- MQTT Protocol Specification: MQTT.org
- Inspired by other MQTT implementations like Mosquitto and HiveMQ