flutter_mqtt 1.0.0+5 copy "flutter_mqtt: ^1.0.0+5" to clipboard
flutter_mqtt: ^1.0.0+5 copied to clipboard

discontinuedreplaced by: dart_mqtt

a mqtt library to easy connect via tcp/ssl,dependence flutter_xtransport,developing,not stable.

Quickstart Guide #

tcp #

import 'package:flutter_mqtt/flutter_mqtt.dart';

void main() async {
  var cli = MqttClient("broker.emqx.io", 1883)
    ..withKeepalive(5)
    ..withClientID("mqtt_test");
  cli.onMqttConack((msg) {
    if (msg.returnCode != MqttConnectReturnCode.connectionAccepted) {
      cli.close();
      return;
    }
    print(msg);
  });
  cli.onBeforeReconnect(() async {
    print("reconnecting...");
  });

  cli.start();
}

copied to clipboard

tls #

import 'dart:io';

import 'package:flutter_mqtt/flutter_mqtt.dart';
import 'package:flutter_xtransport/flutter_xtransport.dart';

void main() async {
  var cli = MqttClient(
    "127.0.0.1",
    8883,
    // reConnectDuration: const Duration(seconds: 5),
    credentials: XtransportCredentials.secure(
      authority: "xx.apple.com",
      certificates: File('cert/ca.pem').readAsBytesSync(),
      clientCertificateBytes: File('cert/client.pem').readAsBytesSync(),
      clientPrivateKeyBytes: File('cert/client.key').readAsBytesSync(),
      onBadCertificate: (_x509, _host) => true,
    ),
  )
    ..withKeepalive(5)
    ..withClientID("mqttx_test");
  cli.onMqttConack((msg) {
    if (msg.returnCode != MqttConnectReturnCode.connectionAccepted) {
      cli.close();
      return;
    }
  });
  cli.onBeforeReconnect(() async{
    print("reconnecting...");
  });
  cli.start();
}
copied to clipboard

Reconnection attributes #

Besides the error and advisory callbacks mentioned above you can also set a few reconnection attributes in the connection options:

  • allowReconnect bool

allowReconnect enables reconnection logic to be used when we encounter a disconnect from the current server. Default is false

  • reconnectWait Duration

reconnectWait sets the time to backoff after attempting to (and failing to) reconnect. Default const Duration(seconds: 2)

  • customReconnectDelayCB Duration Function()?

customReconnectDelayCB is invoked after the library tried every URL in the server list and failed to reconnect. It passes to the user the current number of attempts. This function returns the amount of time the library will sleep before attempting to reconnect again. It is strongly recommended that this value contains some jitter to prevent all connections to attempt reconnecting at the same time.

0
likes
160
points
0
downloads

Publisher

unverified uploader

Weekly Downloads

2024.07.21 - 2025.02.02

a mqtt library to easy connect via tcp/ssl,dependence flutter_xtransport,developing,not stable.

Repository (GitHub)

Documentation

API reference

License

GPL-3.0 (license)

Dependencies

flutter_xtransport

More

Packages that depend on flutter_mqtt