Interesting. I was about to open a new thread for this topic. Lost internet connection is the keyword. Are there best practises to handle this on the smartphone-app using realtime.
Because obviously I am doing it wrong.
I think the SDK should automatically re-connect. Are you saying you don't get reconnected and you don't get any indication it's no longer connected? What's your code?
And are you on flutter web or mobile?
It would be great if re-connecting would be handled by the SDK. But using the android emulator and appwrite on my local machine the SDK does not automatically reconnect realtime. Realtime works fine until I disable and reenable the internet connection in the emulator. The realtime-server still sends events, but they don't reach my code anymore. Neither the onDone() nor the onError() callback of the realtime.subscribe(...).stream.listen() fire. So yes, judging from the appwrite-realtime-server log and the logs from my app, I don't get any indication that the realtime-connection is no longer working.
I am using flutter with appwrite: ^9.0.0 and riverpod. As for my code, there is exactly one realtime instance that subscripes once to all channels I need for the app. I then register and unregister clients to receive events when needed.
I will now try the same code on a real device using a different appwrite installation.
Something strange happened again. After disconnecting and reconnecting the app and confirming that the app does no longer receive realtime events, after some time the appwrite-realtime-server closed the connection. AND the onDone() in my app got called. How does that make any sense? Still on the emulator.
so i have this code:
subscription = realtime.subscribe(['files', 'documents']);
subscription!.stream.listen((data) {
print(data);
setState(() {
realtimeEvent = jsonEncode(data.toMap());
});
}, onError: (e) {
print(e);
}, onDone: () {
print('done');
});
and when i turned on airplaine mode, done
was printed
Hm... onDone() does not get fired for me. I have two thoughts:
- I am killing the internet connection not through airplane mode, but by manually switching off wifi and mobile data. Maybe that is a difference? But onDone() does not get called either.
- I am using connectivity_plus to detect when the app loses internet connection. Maybe that is intercepting the event to propagate further?
I will minimize my code further and see where that gets me.
Let me check by turning off wifi and mobile data
so it also prints done if i turn off mobile data and wifi
What version of appwrite-sdk do you use? I cannot bring onDone() to fire. I see '!! build' and '!! data' on my first test, but no '!! done' when turning off interent, regardless of method. Still on eulator.
Oh, and appwrite 1.3.4
9.0.0. im on a dev version of appwrite but that shouldn't make a difference.
Is this LoginPage widget maybe going away?
also..i wouldn't do this in build π at the very least, it should be in initstate
No difference :(. no '!! dispose' in the log. '!! data' in the log to ensure realtime is working before killing internet connection. I don't think the app ges any more minimal than this.
I am making a new blank app.
Still not working.
- fresh project
- only main.dart
- only dependency is appwrite
- realtime works until first disconnect
- no done, no dispose
- no automatic reconnect
I still use the same appwrite backend and the same project.
Also, Flutter 3.10.2 on Windows
i just tested against cloud and it worked π
All right. As soon as I can (yesterday was a holiday so I had time to tinker) I will test using a real device and an appwrite in the cloud.
Running the minimal-app on a real device against an appwrite in the cloud does indeed trigger onDone() when entering airplane mode. Heaven knows why. Sadly, the realtime-connection is still not reestablished automatically when I leave airplane mode. Should it?
Sadly, the realtime-connection is still not reestablished automatically when I leave airplane mode. Should it? @dlohani?
Not yet, for now you should get the onDone event on the stream you are listening to and you should reconnect yourself. I'm still figuring out a way to do it internally
I've used the https://pub.dev/packages/web_socket_client package and it works reliably. It has a retry function that reliably reconnects when the connection drops
Recommended threads
- Realtime with multiple connections
I need the Realtime on multiple Collections for diffrent applicational logic. So my question is: Is there a way to have only 1 Websocket connection or do I need...
- i am getting 404 not found in my current...
in my current appwrite i am getting 404 not found from backend side and in frontend getting error of CORS error as backend is not working
- How to create a custom authentication sy...
Iβm looking to create an Appwrite function that first verifies a client using some custom logic, like providing a secret key. Once this verification is complete...