Last active
November 10, 2022 01:08
-
-
Save lukepighetti/bac5fb3c67acdc550bf52767513cb41b to your computer and use it in GitHub Desktop.
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 'dart:io'; | |
import 'package:mqtt_client/mqtt_client.dart'; | |
import 'package:mqtt_client/mqtt_server_client.dart'; | |
Future<void> main(List<String> arguments) async { | |
final client = MqttServerClient('mosquitto.local', ''); | |
client.setProtocolV311(); | |
client.keepAlivePeriod = 30; | |
await client.connect(); | |
// subscribe to all topics | |
client.subscribe('#', MqttQos.atMostOnce); | |
await for (final batch in client.updates!) { | |
for (final message in batch) { | |
final p = message.payload; | |
if (p is! MqttPublishMessage) continue; | |
final pt = MqttPublishPayload.bytesToStringAsString(p.payload.message); | |
print('<${message.topic}>: $pt'); | |
} | |
} | |
} |
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
name: pid | |
description: MQTT example | |
version: 1.0.0 | |
environment: | |
sdk: '>=2.18.4 <3.0.0' | |
dependencies: | |
mqtt_client: ^9.7.2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment