shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

MqttClient nut running in a flutter app #353

Closed SiliconEmpire closed 2 years ago

SiliconEmpire commented 2 years ago

import 'dart:io'; import 'package:mqtt_client/mqtt_client.dart'; import 'package:mqtt_client/mqtt_server_client.dart';

main() { MQTTClientWrapper newclient = new MQTTClientWrapper(); newclient.prepareMqttClient(); }

// connection states for easy identification enum MqttCurrentConnectionState { IDLE, CONNECTING, CONNECTED, DISCONNECTED, ERROR_WHEN_CONNECTING }

enum MqttSubscriptionState { IDLE, SUBSCRIBED }

class MQTTClientWrapper {

MqttClient client;

MqttCurrentConnectionState connectionState = MqttCurrentConnectionState.IDLE;
MqttSubscriptionState subscriptionState = MqttSubscriptionState.IDLE;

// using async tasks, so the connection won't hinder the code flow void prepareMqttClient() async { _setupMqttClient(); await _connectClient(); //_subscribeToTopic('Dart/Mqtt_client/testtopic'); // _publishMessage('Hellodsfsdfdsg'); }

// waiting for the connection, if an error occurs, print it and disconnect
Future<void> _connectClient() async {
  try {
    print('client connecting....');
    connectionState = MqttCurrentConnectionState.CONNECTING;
    await client.connect('', '');
  } on Exception catch (e) {
    print('client exception - $e');
    connectionState = MqttCurrentConnectionState.ERROR_WHEN_CONNECTING;
    client.disconnect();
  }

  // when connected, print a confirmation, else print an error
  if (client.connectionStatus.state == MqttConnectionState.connected) {
    connectionState = MqttCurrentConnectionState.CONNECTED;
    print('client connected');
  } else {
    print(
        'ERROR client connection failed - disconnecting, status is ${client.connectionStatus}');
    connectionState = MqttCurrentConnectionState.ERROR_WHEN_CONNECTING;
    client.disconnect();
  }
}

void _setupMqttClient() { client = MqttServerClient.withPort('*****.s1.eu.hivemq.cloud', 'ujygh', 8883); // the next 2 lines are necessary to connect with tls, which is used by HiveMQ Cloud client.secure = true; client.securityContext = SecurityContext.defaultContext; client.keepAlivePeriod = 20; client.onDisconnected = _onDisconnected; client.onConnected = _onConnected; client.onSubscribed = _onSubscribed; }

void _subscribeToTopic(String topicName) { print('Subscribing to the $topicName topic'); client.subscribe(topicName, MqttQos.atMostOnce);

// print the message when it is received
client.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) {
  final MqttPublishMessage recMess = c[0].payload;
  var message = MqttPublishPayload.bytesToStringAsString(recMess.payload.message);

  print('YOU GOT A NEW MESSAGE:');
  print(message);
});

}

void _publishMessage(String message) { final MqttClientPayloadBuilder builder = MqttClientPayloadBuilder(); builder.addString(message);

print('Publishing message "$message" to topic ${'Dart/Mqtt_client/testtopic'}');
client.publishMessage('Dart/Mqtt_client/testtopic', MqttQos.exactlyOnce, builder.payload);

}

// callbacks for different events void _onSubscribed(String topic) { print('Subscription confirmed for topic $topic'); subscriptionState = MqttSubscriptionState.SUBSCRIBED; }

void _onDisconnected() { print('OnDisconnected client callback - Client disconnection'); connectionState = MqttCurrentConnectionState.DISCONNECTED; }

void _onConnected() { connectionState = MqttCurrentConnectionState.CONNECTED; print('OnConnected client callback - Client connection was sucessful'); }

}` i have been trying to run the above code from Hiv mq example for dart in a flutter app, its not working for me but when i run it independently it works please what am i doing wrong

SiliconEmpire commented 2 years ago

thanks i just ran same app on mobile and its working pretty well