Skip to content

Instantly share code, notes, and snippets.

View pedrocganem's full-sized avatar
🏠
Working from home

Pedro Ganem pedrocganem

🏠
Working from home
  • Ecobot
  • Curitiba - PR
  • 02:21 (UTC -03:00)
View GitHub Profile
import 'package:flutter/material.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_flutter/mqtt.dart';
void main() {
runApp(MaterialApp(home: HomeScreen()));
}
class HomeScreen extends StatefulWidget {
@override
Future<MqttServerClient> brokerSetup(Function function) async {
client = MqttServerClient.withPort(broker, clientIdentifier, port);
client.logging(on: true);
client.onConnected = onConnected;
client.onDisconnected = onDisconnected;
client.onSubscribed = onSubscribed;
client.onSubscribeFail = onSubscribeFail;
client.pongCallback = pong;
client.secure = false;
String broker = 'your_broker';
int port = 1883;
String username = 'your_username';
String password = 'your_password';
String clientIdentifier = 'your_client_identifier';
MqttServerClient client;
StreamSubscription subscription; //* I'll explain this later!
import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart';
class Broker {
static final Broker _singleton = Broker._internal();
factory Broker() {
return _singleton;
}
dependencies:
flutter:
sdk: flutter
mqtt_client: ^7.3.0