This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dependencies: | |
| flutter: | |
| sdk: flutter | |
| mqtt_client: ^7.3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |