Implementing MQTT in Flutter

倖福魔咒の 提交于 2021-02-07 07:19:43

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/51836505/implementing-mqtt-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!