Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing MQTT in Flutter

I am new to Flutter (coming from Android background) and want to implement MQTT client in flutter.

This is what i want:

  1. MQTT client which should be "alive" when the app is open/not killed by OS.

  2. I don't want to run it in background (without the app open, i know there is issue doing it in iOS, read it somewhere) but while the app is open.

  3. Updating UI/State based on messages coming in subscribed topics.

I looked into pub and found 2 packages, mqtt and mqtt_client, they look like non-Flutter implementation of MQTT in dart.

I have an idea how to use one of this lib to make it work with flutter but not sure if it will work:

  1. Create and implement MQTTObserver sateful widget which will async-ly connect to broker and listen for messages to particular topic and update the state accordingly.

  2. Will wrap this widget around MaterialApp

  3. Any widget down the tree would be able to access the state of MQTTObserver widget to reflect it in their UI.

Do you think it is a good idea? will it work? do you have better idea? or should i implement platform-specific implementation for MQTT (should be my last option).

like image 560
Harsh Bhikadia Avatar asked Aug 14 '18 08:08

Harsh Bhikadia


1 Answers

The mqtt_client package is already being used in flutter although under development, I don't use flutter myself but several flutter users have raised issues on it, see [here][1]

[1]: https://github.com/shamblett/mqtt_client/issues/16 for an example, there others in the issues list, maybe you could ask one of these flutter devs your question.

like image 154
user2685314 Avatar answered Sep 22 '22 20:09

user2685314