mqtt 1.1.4

  • Readme
  • Changelog
  • Installing
  • 27

Mqtt #

This is an mqtt client written in DART. It can be used in the browser (over websocket) or the VM (over socker or websocket).

See for details on mqtt protocol

Usage #

Create a connection over websocket: var mqttCnx = new MqttConnectionIOWebSocket.setOptions("ws://");

Create a connection over socket: var mqttCnx = new MqttConnectionIOSocket.setOptions(host:, port: 8083);

Create mqtt client: MqttClient c = new MqttClient(mqttCnx, clientID: "MyClientID", qos: QOS_1);

Set a will (must be done before connecting to the broker): c.setWill("MyWillTopic", "MyWillPayload", QOS_1, 0);

Connect to broker: c.connect(onConnectionLost) .then( (c)=> onConnected(c) ) .catchError((e) => print("Error: $e"), test: (e) => e is SocketException)
.catchError((mqttErr) => print("Error: $mqttErr") );

Publish a message: c.publish("MyTopic", "MyMessage", 1, QOS_1, 0) .then( (m) => print("Message ${m.messageID} published"); );

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

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

Disconnect: c.disconnect();

VM Example #

See mqtt_sub for a sample mqtt publish and mqtt_pub for a sample subscribe. Available options can be displayed through: > dart mqtt_sub.dart -h > dart mqtt_pub.dart -h

Web Example #

See Mqtt_web for a sample web page connecting, subscribing and publishing mqtt messages

Testing #

Testing was done using the mosquitto broker ( A mosquitto test server is available at

Websocket testing was done throught mqtt_ws_bridge.dart.

1.1.4 #

  • Dart 2 support

1.1.3 #

  • Fix error "Connection Refused: bad user name or password"
  • Fix issue with dart 2 support: "type 'Future

1.1.2 #

  • Dart 2 support

1.0.1 #

  • Initial version

Use this package as a library

1. Depend on it

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

  mqtt: ^1.1.4

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:mqtt/mqtt_client.dart';
import 'package:mqtt/mqtt_connection_html_websocket.dart';
import 'package:mqtt/mqtt_connection_io_socket.dart';
import 'package:mqtt/mqtt_connection_io_websocket.dart';
import 'package:mqtt/mqtt_connection_shared.dart';
import 'package:mqtt/mqtt_message.dart';
import 'package:mqtt/mqtt_message_assured.dart';
import 'package:mqtt/mqtt_message_connack.dart';
import 'package:mqtt/mqtt_message_connect.dart';
import 'package:mqtt/mqtt_message_disconnect.dart';
import 'package:mqtt/mqtt_message_ping.dart';
import 'package:mqtt/mqtt_message_publish.dart';
import 'package:mqtt/mqtt_message_suback.dart';
import 'package:mqtt/mqtt_message_subscribe.dart';
import 'package:mqtt/mqtt_message_unsuback.dart';
import 'package:mqtt/mqtt_message_unsubscribe.dart';
import 'package:mqtt/mqtt_options.dart';
import 'package:mqtt/mqtt_shared.dart';
import 'package:mqtt/mqtt_utils.dart';
import 'package:mqtt/mqtt_version_v3.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Fix dependencies in pubspec.yaml.

Running pub upgrade failed with the following output:

ERR: The current Dart SDK version is 2.7.1.
 Because mqtt depends on ini >=0.0.5 <2.0.0 which requires SDK version <2.0.0, version solving failed.

Health suggestions

Format bin/mqtt_ws_bridge/mqtt_ws.dart.

Run dartfmt to format bin/mqtt_ws_bridge/mqtt_ws.dart.

Format lib/mqtt_client.dart.

Run dartfmt to format lib/mqtt_client.dart.

Format lib/mqtt_connection_html_websocket.dart.

Run dartfmt to format lib/mqtt_connection_html_websocket.dart.

Fix additional 18 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/mqtt_connection_io_socket.dart (Run dartfmt to format lib/mqtt_connection_io_socket.dart.)
  • lib/mqtt_connection_io_websocket.dart (Run dartfmt to format lib/mqtt_connection_io_websocket.dart.)
  • lib/mqtt_connection_shared.dart (Run dartfmt to format lib/mqtt_connection_shared.dart.)
  • lib/mqtt_message.dart (Run dartfmt to format lib/mqtt_message.dart.)
  • lib/mqtt_message_assured.dart (Run dartfmt to format lib/mqtt_message_assured.dart.)
  • lib/mqtt_message_connack.dart (Run dartfmt to format lib/mqtt_message_connack.dart.)
  • lib/mqtt_message_connect.dart (Run dartfmt to format lib/mqtt_message_connect.dart.)
  • lib/mqtt_message_disconnect.dart (Run dartfmt to format lib/mqtt_message_disconnect.dart.)
  • lib/mqtt_message_ping.dart (Run dartfmt to format lib/mqtt_message_ping.dart.)
  • lib/mqtt_message_publish.dart (Run dartfmt to format lib/mqtt_message_publish.dart.)
  • lib/mqtt_message_suback.dart (Run dartfmt to format lib/mqtt_message_suback.dart.)
  • lib/mqtt_message_subscribe.dart (Run dartfmt to format lib/mqtt_message_subscribe.dart.)
  • lib/mqtt_message_unsuback.dart (Run dartfmt to format lib/mqtt_message_unsuback.dart.)
  • lib/mqtt_message_unsubscribe.dart (Run dartfmt to format lib/mqtt_message_unsubscribe.dart.)
  • lib/mqtt_options.dart (Run dartfmt to format lib/mqtt_options.dart.)
  • lib/mqtt_shared.dart (Run dartfmt to format lib/mqtt_shared.dart.)
  • lib/mqtt_utils.dart (Run dartfmt to format lib/mqtt_utils.dart.)
  • lib/mqtt_version_v3.dart (Run dartfmt to format lib/mqtt_version_v3.dart.)

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.

Package is getting outdated. (-63.01 points)

The package was last published 85 weeks ago.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and mqtt.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0