universal_mqtt_client 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • new76

universal_mqtt_client #

Pub Version

A MQTT client for Dart that works on any Dart supported compile target (including Flutter and Flutter Web).

Features #

  • Full support for MQTT 3.3.1
  • Support for MQTT over WebSocket on all Dart compile targets (including web)
  • Support for MQTT over TCP on mobile and desktop
  • Dart idiomatic API using promises and streams
  • Built in reconnect functionality
  • Full support for wildcard topics

Usage #

/// To use this example you need to have a WebSocket `mqtt` server
/// running on localhost:9000.

import 'package:universal_mqtt_client/universal_mqtt_client.dart';

void main() async {
  // Create a new UniversalMqttClient. This does not start the connection yet.
  final client = UniversalMqttClient(broker: Uri.parse('ws://localhost:9000'));
  client.status.listen((status) {
    print('Connection Status: $status');
  });

  // We now call `client.connect()` to establish a connection with the MQTT broker.
  // The returned promise resolves when the connection is successful, a timeout
  // has been reached, or the broker responds with an error.
  await client.connect();

  // We now subscribe to the client and save the returned StreamSubscription
  final subscription = client
      .handleString('device_status/1', MqttQos.atLeastOnce)
      .listen((message) => print('Device 1 Status: $message'));

  // then publish a message to the topic we subscribed to.
  client.publishString(
      'device_status/1', 'Connected and running!', MqttQos.atLeastOnce);

  // Then we wait a bit before we cancel our subscription.
  await Future.delayed(Duration(seconds: 2));

  // Now we can cancel our subscription. This means that any messages after this will
  // not be recieved by the client anymore.
  await subscription.cancel();

  // Ultimatly we clean up our connection by disconnecting from the broker.
  client.disconnect();
}

Bugs and feature requests #

Please file feature requests and bugs at the issue tracker. If you find any security related issues, please follow the guidance in the SECURITY.md file. Thanks :-)

Licence #

This project is licensed under the MIT licence. More information can be found in the LICENCE file.

0.1.1 #

  • fix: dartdoc fails to generate documentation

0.1.0 #

  • Initial release

example/main.dart

/// To use this example you need to have a WebSocket `mqtt` server
/// running on localhost:9000.

import 'package:universal_mqtt_client/universal_mqtt_client.dart';

void main() async {
  // Create a new UniversalMqttClient. This does not start the connection yet.
  final client = UniversalMqttClient(broker: Uri.parse('ws://localhost:9000'));
  client.status.listen((status) {
    print('Connection Status: $status');
  });

  // We now call `client.connect()` to establish a connection with the MQTT broker.
  // The returned promise resolves when the connection is successful, a timeout
  // has been reached, or the broker responds with an error.
  await client.connect();

  // We now subscribe to the client and save the returned StreamSubscription
  final subscription = client
      .handleString('device_status/1', MqttQos.atLeastOnce)
      .listen((message) => print('Device 1 Status: $message'));

  // then publish a message to the topic we subscribed to.
  client.publishString(
      'device_status/1', 'Connected and running!', MqttQos.atLeastOnce);

  // Then we wait a bit before we cancel our subscription.
  await Future.delayed(Duration(seconds: 2));

  // Now we can cancel our subscription. This means that any messages after this will
  // not be recieved by the client anymore.
  await subscription.cancel();

  // Ultimatly we clean up our connection by disconnecting from the broker.
  client.disconnect();
}

Use this package as a library

1. Depend on it

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


dependencies:
  universal_mqtt_client: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:universal_mqtt_client/universal_mqtt_client.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
57
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Maintenance issues and suggestions

Documentation URL isn't helpful. (-10 points)

Either remove the documentation field from pubspec.yaml, or update it to link to a website about the package.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.8.1 <3.0.0
meta ^1.1.8 1.2.2 1.3.0-nullsafety
mqtt_client ^7.2.1 7.2.1
rxdart ^0.24.1 0.24.1
uuid ^2.1.0 2.2.0
Transitive dependencies
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
convert 2.1.1
crypto 2.1.5
event_bus 1.1.1
path 1.7.0
typed_data 1.2.0 1.3.0-nullsafety
Dev dependencies
pedantic ^1.9.0
platform_detect ^1.4.0
stream_channel ^2.0.0
test ^1.14.4