shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
49 stars 25 forks source link

Problem to conect Flutterflow an #83

Closed bytonycruz closed 6 months ago

bytonycruz commented 7 months ago

I have a problem implementing the library, because when trying to connect to my broker it gives me an error, I use the Flutterflow platform so I am only implementing a bit of segmented code but I can't connect

// Automatic FlutterFlow imports
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

// MQTT imports
import 'package:mqtt5_client/mqtt5_client.dart';
import 'package:mqtt5_client/mqtt5_browser_client.dart';

// Crear una instancia del cliente MQTT
final client = MqttBrowserClient('wss://l833b1ce.emqx.cloud', '');

// Función para realizar la conexión MQTT
Future<String> mqttConnect() async {
  // Configuración del cliente MQTT
  client.logging(on: false);
  client.port = 8083;

  // Configuración del mensaje de conexión MQTT
  final connMess = MqttConnectMessage()
      .withClientIdentifier('emqx_app9dcd96')
      .startClean()
      .keepAliveFor(60)
      .withWillQos(MqttQos.atLeastOnce);

  // Asignar el mensaje de conexión al cliente
  client.connectionMessage = connMess;

  try {
    // Intentar conectar al broker MQTT
    await client.connect('user', 'password');
    return 'Conexión exitosa'; // Devolver éxito si la conexión fue exitosa
  } catch (e) {
    // Manejar excepción en caso de error durante la conexión
    print('Exception: $e');
    client.disconnect();
    return 'Error en la conexión: ${e.toString()}'; // Devolver error si hubo un problema en la conexión
  }
}

Screenshot 2024-01-19 013639

shamblett commented 7 months ago

What error does it give you?

Try removing the will qos from the connection message.

A log would be useful