shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
550 stars 179 forks source link

Hi, how to make the mqtt listen loop runs in isolate #562

Open MonolithFoundation opened 1 month ago

MonolithFoundation commented 1 month ago

I recently found, the apps would get janky when network is not stable or slow. I first think it was from flutter, but the simplest page transition would janky as well, then I thought it might comes from my mqtt loop.

We learned that flutter actually single thread, which means any loop runs in main UI thread would caused janky.

Is that possible make mqtt listen loop runs in isolate, while the messages arrives could also be listen and writes into sql and can ware in main thread so that UI gets update?

shamblett commented 1 month ago

I'm not a flutter user so I don't know exactly what flutter can offer but Dart has isolates as you know, you could put your whole mqtt code in its own isolate and just pass subscriptions/incoming messages to/from it, or you could maybe just put the listen loop in its own isolate, its up to you really.

Other flutter users may be able to help you better with this, the client has no in built support for isolating parts of its code.

MonolithFoundation commented 1 month ago

@shamblett Yes, but we hopefully mqtt_client could treat flutter as higher priority since this is the only lib with dart can be used in flutter supports mqtt.

Using isolates would means the whole operation are apart from main thread, the changes won't make UI update instantly.

Same issue actually existed as well, for example, the drfit sqlite client in dart, they offered an implemetation with isolate support built-in.

I have to say, flutter is cubersome, not dart, not mqtt_client, really apperaciated your kindly opensource.

But we have get into this siutaiton, flutter is single threaded, every heavy operation would caused main UI thread janky. Any thoughts on how to make mqtt more smooth on UI side?

shamblett commented 1 month ago

Unfortunately the client can't favour flutter, it is a Dart library, if you want to use it in flutter in a certain way then you are free to do so but that's up to you. As I've said above I'm sure other flutter users have encountered the same problem as yourselves and overcome it.

There is this package that specialises the client for use in flutter. This may or may not help you.