MQTT Client Library Encyclopedia – MQTT.DART

Written by Jean-Noel Guillerme

Category: MQTT MQTT Client Library

Published: October 5, 2015




Short info

MQTT.DART
Language Dart
Website https://github.com/jnguillerme/mqtt.dart

Description

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.

Features

Feature
MQTT 3.1 ok
MQTT 3.1.1 nok
LWT ok
SSL/TLS nok
Automatic Reconnect nok
Feature
QoS 0 ok
QoS 1 ok
QoS 2 ok
Authentication ok
Throttling nok


Usage

Installation

Add this to your package’s pubspec.yaml file:

1
2
dependencies:
  mqtt: "^1.1.0"

You can install packages from the command line:

1
$ pub get

In your dart code add:

1
import 'package:mqtt/mqtt_shared.dart';

Connect

1.Define a connection Before connecting to the broker you must define a connection. Web socket and socket connections are supported:

websocket:

1
var mqttCnx = new MqttConnectionIOWebSocket.setOptions("ws://127.0.0.1/8080");

socket:

1
var mqttCnx = new MqttConnectionIOSocket.setOptions(host:127.0.0.1, port: 8083);

2.Create a client

1
MqttClient c = new MqttClient(mqttCnx, clientID: "MyClientID", qos: QOS_1);

3.Connect

1
2
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:

1
var mqttCnx = new MqttConnectionIOWebSocket.setOptions("ws://127.0.0.1/8080");

socket:

1
var mqttCnx = new MqttConnectionIOSocket.setOptions(host:127.0.0.1, port: 8083);

2. Create a client

1
MqttClient c = new MqttClient(mqttCnx, clientID: "MyClientID", qos: QOS_1);

3. Define the will

1
c.setWill("MyWillTopic", "MyWillPayload", QOS_1, 0);

4. Connect

1
2
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 :

1
MqttClient c = new MqttClient(mqttCnx, clientID: "MyClientID", qos: QOS_1, userName: MyUserName, password: MyPassword);

Publish

1
2
c.publish("MyTopic", "MyMessage")
 	.then( (m) => print("Message ${m.messageID} published"); );

Publish a retained message

1
2
c.publish("MyTopic", "MyMessage", retain: true)
 	.then( (m) => print("Message ${m.messageID} published"); );

Subscribe

1
2
c.subscribe("MyTopic", QOS_1, onMessage) 
.then( (s) => print("Subscription done - ID: ${s.messageID} - Qos: ${s.grantedQoS}") );

Unsubscribe

1
2
c.unsubscribe("MyTopic", s.messageID) 
	.then( (u) => print("Unsubscribed from subscription ${u.messageID}") );

Disconnect

1
c.disconnect();

About Jean-Noel Guillerme

I work as an independent software developer in London. I’ve been working on projects around messaging for more than 10 years. I started working with MQTT a couple of years ago, when I was involved in mobile app projects.

Website

<  MQTT Client Library Encyclopedia – Paho Python   |   MQTT Client Library Encyclopedia – MQTT.js   >