Hello FlutterFlow Community,
I'm currently working on integrating MQTT functionality within my FlutterFlow project using a custom action. I've implemented a custom function to connect to an MQTT broker and publish messages. However, I'm facing issues where the connection does not seem to establish properly, and the publish action is not successful. There are no errors or success messages returned from the function either.
Problem Details:
I am using the mqtt_client package in my custom action code.
My custom action is triggered on a button click, and I display the returned message using a Snackbar.
Despite setting the correct broker address, topic, and message, the MQTT connection is not being established successfully.
The code does not return any errors, and no messages are published to the topic.
My Custom Function Code:
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart';
import '/flutter_flow/custom_functions.dart';
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart';
Future<String> mqttFunction(
String brokerAddress,
String publishMessage,
String brokerTopic,
) async {
final client = MqttServerClient(brokerAddress, '');
client.port = 1883;
// Disable logging for cleaner output
client.logging(on: true);
client.keepAlivePeriod = 20;
// Set up connection callbacks
client.onDisconnected = () {
print('OnDisconnected - Client disconnection');
if (client.connectionStatus != null) {
print(
'Disconnection origin: ${client.connectionStatus!.disconnectionOrigin}');
}
};
client.onConnected = () {
print('onConnected - Client connection was successful');
};
// Connection message setup
final connMess = MqttConnectMessage()
.withClientIdentifier('dart_client')
.withWillTopic('willtopic')
.withWillMessage('My Will message')
.startClean()
.withWillQos(MqttQos.atLeastOnce);
client.connectionMessage = connMess;
print('Client connecting to $brokerAddress');
try {
await client.connect();
} on Exception catch (e) {
print('Client exception - $e');
client.disconnect();
return 'Client exception occurred: $e';
}
// Check connection status
if (client.connectionStatus == null ||
client.connectionStatus!.state != MqttConnectionState.connected) {
final statusMessage = client.connectionStatus == null
? 'Client connection failed - Status is null'
: 'Client connection failed - Status: ${client.connectionStatus}';
print(statusMessage);
client.disconnect();
return statusMessage;
}
print('Client connected');
final builder = MqttClientPayloadBuilder();
builder.addString(publishMessage);
print('Publishing message to topic: $brokerTopic');
try {
client.publishMessage(brokerTopic, MqttQos.atMostOnce, builder.payload!);
} catch (e) {
print('Message publishing failed - $e');
client.disconnect();
return 'Message publishing failed: $e';
}
print('Message published successfully');
// Disconnect the client after successful publishing
client.disconnect();
print('Client disconnected successfully');
return 'Message published successfully to topic: $brokerTopic';
}