felangel / web_socket_client

A simple WebSocket client for Dart which includes automatic reconnection logic.
https://pub.dev/packages/web_socket_client
MIT License
150 stars 32 forks source link

fix: Websocket not updating state connection #43

Closed martinpelli closed 3 months ago

martinpelli commented 6 months ago

Description

Hi, didn't know how to classify this, maybe is a bug, maybe a question or maybe a documentation problem.

I have an app that needs a behavior like Whatsapp or any messenger app, that when you open the app it should always update the screen if there is new data. So when using this websocket all is good while I am on the app. But when I go to another app or block the device for a long time, websocket of course loose connection maybe because of how Android or iOS handles background calls. So there are some times that websocket is not able to reconnect, I mean is not even noticing it (using the listener for state connection). Idk if this is a expected behavior when you leave the app? How I can handle this? I am helping me with WidgetsBindingObserver, but as I don't know if the websocket looses connection I don't have a way to know if a call to remote data source is needed. Another question that I couldn't find is the amount of time that strategies tries to reconnect, is infinite? I am using the default one.

This is happening only on a physical device, I am not able to reproduce it on emulator. No matter if I lock the device for hours, websocket state connection works great. But there is a second problem related: disabling wifi and enabling, happens in both, physical and emulator devices, the listener is not fired.

Expected Behavior listener should always communicate the state of the connection? How to handle when you go out of the app, and come back, listener should reconnect? If you disable internet connection like Wifi, and re enable it, webscoket should reconnect? Or at least communicate it on the listener?

felangel commented 3 months ago

Hi @martinpelli 👋 Thanks for opening an issue!

This is expected as the operating system will kill apps in the background after they have been paused for a while. You likely need to support silent notifications in addition to a socket connection in order to achieve the behavior you described.

Hope that helps! Closing for now but if you have any follow up questions let me know and I'm happy to continue the conversation 👍

kanczler commented 3 months ago

The second issue in the original post was not addressed: "But there is a second problem related: disabling wifi and enabling, happens in both, physical and emulator devices, the listener is not fired.". I have experienced this as well, the listener does not fire when there's no internet (and neither upon reconnecting).