shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
548 stars 176 forks source link

Subscription in background #194

Closed chiragmittal19 closed 3 years ago

chiragmittal19 commented 4 years ago

It would be good to have a way to subscribe to a topic in a background service. The device will be able to receive events even after the app is killed.

shamblett commented 4 years ago

Not sure what you mean by a 'background service'. The client provides a subscription API which you can use whenever you need to presumably, what functionality do you think the client is missing?

chiragmittal19 commented 4 years ago

When the app is killed, socket gets disconnected. Can there be a way to keep the socket connected and receive events?

shamblett commented 4 years ago

There is the autoreconnect functionality available, if the client gets a disconnect indication from the broker socket it will try and reconnect automatically, however re-connection is of course dependent on the network availability, the client can't really control this.

What do you mean by 'when the app is killed'? How would the client keep the socket connected if its been killed?

chiragmittal19 commented 4 years ago

I meant initializing the client in a background service which will keep on running even after the app is killed.

shamblett commented 4 years ago

So what stops you doing this?

chiragmittal19 commented 4 years ago

I guess there is no way to keep a service running in the background continously, in Flutter. Maybe not an issue in mqtt_client then.

justintime4tea commented 4 years ago

@chiragmittal19 there's a hacky way using FlutterNativeView to run dart code in a background isolate. We are doing this rather successfully however it relies on a foreground service because background services aren't gaurunteed to run forever. If you need details message me I can dig up the links for references.

uwejan commented 4 years ago

Hi, @justintime4tea Can you please share some code on how to do so in our case with mqtt_client here.

chiragmittal19 commented 4 years ago

@chiragmittal19 there's a hacky way using FlutterNativeView to run dart code in a background isolate. We are doing this rather successfully however it relies on a foreground service because background services aren't gaurunteed to run forever. If you need details message me I can dig up the links for references.

Yeah, please provide us with some references. I don't know what but something stops me from being confident with this idea.

justintime4tea commented 4 years ago

@uwejan, @chiragmittal19

We essentially followed this article with a few tweaks

“The Tricky Task of Keeping Flutter Running (Vol. 2)” by Pau Picas https://link.medium.com/vKb6F0vUo9

xhidnoda commented 4 years ago

Can you try this?

https://github.com/shamblett/mqtt_client/issues/219