web_socket_client icon indicating copy to clipboard operation
web_socket_client copied to clipboard

feat: reconnect on network change or app wake

Open tankadesign opened this issue 2 years ago • 6 comments

I have used this package to replace some custom reconnect code because I think it does a better job than we were doing. However one issue with the this package is that when the network switches, from Wifi to Cellular for example, there is no reconnection attempt made because often the WebSocket class doesn't register it as a dropped connection. My fix was to use the connectivity_plus package to look for changes to the network, ignoring changes to and from no connection since this package already handles disconnects, to create a reliable long running websocket connection.

There are also issues where a websocket connection will stop responding after the app has gone into the background but hasn't been unloaded from memory. In this case we need to check if the app has resumed and assume that after a certain amount of time it has been paused that we should force a reconnect.

It would be great if this client could handle those cases, because maintaining a reliable websocket connection in flutter isn't easy.

tankadesign avatar Jan 13 '23 19:01 tankadesign

Hi @tankadesign 👋 Thanks for opening an issue!

I should have some time to take a look at this in the next few days. Ideally, the solution we come up with should not depend on Flutter since this package is currently pure Dart but I agree this is a use-case we should address 👍

If it's possible, it would be great if you could share some samples of the solutions you described to use as a reference, thanks!

felangel avatar Jan 13 '23 21:01 felangel

Great, thanks for taking the suggestion. I figure at v0.1.0 there's room to grow 😀.

motiapp avatar Jan 14 '23 02:01 motiapp

Hey @felangel and @tankadesign. We've had a few issues with handling reconnections using connectivity_plus package as it sometimes reports invalid connection states when using a VPN (see https://github.com/fluttercommunity/plus_plugins/issues/857, https://github.com/fluttercommunity/plus_plugins/issues/1444).

I would suggest looking into internet_connection_checker for reading internet connection states as it seems to work more reliably. The way we currently approach the socket reconnection is by listening to InternetConnectionChecker.onStatusChange and attempting to reconnect when the status changes from disconnected to connected. Hope that makes sense.

Good work with the package @felangel!

bselwe avatar Jan 18 '23 21:01 bselwe

Hey @felangel and @tankadesign. We've had a few issues with handling reconnections using connectivity_plus package as it sometimes reports invalid connection states when using a VPN (see fluttercommunity/plus_plugins#857, fluttercommunity/plus_plugins#1444).

I would suggest looking into internet_connection_checker for reading internet connection states as it seems to work more reliably. The way we currently approach the socket reconnection is by listening to InternetConnectionChecker.onStatusChange and attempting to reconnect when the status changes from disconnected to connected. Hope that makes sense.

Good work with the package @felangel!

Thanks for sharing @bselwe! I agree that seems to be a much more reliable and platform-agnostic solution 💯

felangel avatar Jan 18 '23 21:01 felangel

@bselwe Thanks for the info, I'll check out the package. In the case where networks change from WiFi to Cellular, or vice-versa, do you know if it always reports disconnected in between the switch? I wonder if it behaves the same if you manually change it, by turning wifi on and off, versus when you move out of range of wifi.

tankadesign avatar Jan 18 '23 22:01 tankadesign

@tankadesign I think it depends on how quickly your network changes between WiFi and Cellular. In the default configuration, the package automatically checks for the internet connection every 10 seconds. So if the automatic check happens during the network change, you might experience disconnected state for a while.

bselwe avatar Jan 18 '23 22:01 bselwe

Any update on this?

mcmah309 avatar Dec 28 '24 20:12 mcmah309