MQTT.DART - MQTT Client Library
I developed the MQTT Dart library as I was working on the backend of a mobile app in 2013. The backend was developed in Dart but I could not find any MQTT library.
I’ve not been too active on the library recently due to lack of time. But I have plan to review the library with the latest Dart features and include the missing MQTT features before the end of the year.
Overview of MQTT.DART MQTT Client Library
MQTT.DART | |
---|---|
Language | Dart |
Website | github.com/jnguillerme/mqtt.dart |
Features Supported by MQTT.DART MQTT Client Library
Feature | |
---|---|
MQTT 3.1 | Yes |
MQTT 3.1.1 | No |
LWT | Yes |
SSL/TLS | No |
Automatic Reconnect | No |
Advanced Features: Supported by MQTT.DART MQTT Client Library
Feature | |
---|---|
QoS 0 | Yes |
QoS 1 | Yes |
QoS 2 | Yes |
Authentication | Yes |
Throttling | No |
Usage of MQTT.DART MQTT Client Library
How to Install the MQTT.DART MQTT Client Library?
Add this to your package’s pubspec.yaml file:
dependencies:
mqtt: "^1.1.0"
You can install packages from the command line:
$ pub get
In your dart code add:
import 'package:mqtt/mqtt_shared.dart';
How to Connect an MQTT Broker to MQTT.DART MQTT Client Library
1.Define a connection before connecting to the MQTT broker you must define a connection. Web socket and socket connections are supported:
websocket:
var mqttCnx = new MqttConnectionIOWebSocket.setOptions("ws://127.0.0.1/8080");
socket:
var mqttCnx = new MqttConnectionIOSocket.setOptions(host:127.0.0.1, port: 8083);
2.Create a client
MqttClient c = new MqttClient(mqttCnx, clientID: "MyClientID", qos: QOS_1);
3.Connect
c.connect(onConnectionLost) .then( (c)=> onConnected(c) ) .catchError((e) => print("Error: $e"), test: (e) => e is SocketException)
.catchError((mqttErr) => print("Error: $mqttErr") );
Connect with LWT
1. Define a connection Before connecting to the broker you must define a connection. Web socket and socket connections are supported:
websocket:
var mqttCnx = new MqttConnectionIOWebSocket.setOptions("ws://127.0.0.1/8080");
socket:
var mqttCnx = new MqttConnectionIOSocket.setOptions(host:127.0.0.1, port: 8083);
2. Create a client
MqttClient c = new MqttClient(mqttCnx, clientID: "MyClientID", qos: QOS_1);
3. Define the will
c.setWill("MyWillTopic", "MyWillPayload", QOS_1, 0);
4. Connect
c.connect(onConnectionLost) .then( (c)=> onConnected(c) ) .catchError((e) => print("Error: $e"), test: (e) => e is SocketException)
.catchError((mqttErr) => print("Error: $mqttErr") );
Connect with Username/ Password
Create the client as:
MqttClient c = new MqttClient(mqttCnx, clientID: "MyClientID", qos: QOS_1, userName: “MyUserName”, password: ”MyPassword”);
Publish
c.publish("MyTopic", "MyMessage")
.then( (m) => print("Message ${m.messageID} published");
Publish a retained message
c.publish("MyTopic", "MyMessage", retain: true)
.then( (m) => print("Message ${m.messageID} published"); );
Subscribe
c.subscribe("MyTopic", QOS_1, onMessage)
.then( (s) => print("Subscription done - ID: ${s.messageID}
Unsubscribe
c.unsubscribe("MyTopic", s.messageID)
.then( (u) => print("Unsubscribed from subscription ${u.messageID}") );
Disconnect
c.disconnect();