Back

Flutter Realtime inactive after some time due to phone beingidle

  • 1
  • Flutter
  • Realtime
godfather
27 Feb, 2023, 17:49

As i don't want to download everytime mine app come to foreground (i.e. user shift to mine app from someother app)

TL;DR
The user is experiencing an issue where the Flutter Realtime feature becomes inactive after the phone is idle for some time. They have tried updating the package file and have attempted to use `onError` and `onDone` callbacks, but neither worked. Another user suggests checking the code for any errors and verifies that the suggested changes to the code should fix the issue. The user asks if they can directly change the source file of Flutter and release it as the latest version. They are advised to fork the SDK and apply the patch to their version of the SDK. The issue is identified as an older version of the SDK not closing the realtime stream
Drake
27 Feb, 2023, 17:51

realistically, not. iOS and Android can close background apps at any time.

You should be able to hook into lifecyle of your app using flutter. The realtime subscription should also have an onDone callback

godfather
27 Feb, 2023, 17:55

So when ondone is called, i can restart the connection again to keep it alive?

Drake
27 Feb, 2023, 17:58

you can try...but again, don't expect it to stay connected if the app isn't in the foreground

godfather
27 Feb, 2023, 18:01

Oh. Okay so it will again be killed. Better approach will be to check if it was closed and if yes then download the data when user comes again on foreground ?

Drake
27 Feb, 2023, 18:05

you can either:

  1. use push notifications to send data to the device
  2. have a background job that tries to sync the data
godfather
27 Feb, 2023, 18:07

Ok. Thanks πŸ‘πŸ»

godfather
1 Mar, 2023, 18:09

How to check if a realtime onnection is still active or not? When I closed the subscription it didn't gave any error but realtime was not active

Drake
1 Mar, 2023, 18:11

I don't think the underlying websocket connection is exposed

godfather
1 Mar, 2023, 18:12

I didn't understand what you mean. Can please explain more?

godfather
1 Mar, 2023, 18:15

So, my app was inactive for sometime and I updated a data to check realtime. But it didn't get updated in frontend. So subscribtion.close should have thrown the error which would have refreshed the data (i put it inside the error, so that if realtime was not active it should have refreshed the data). But it didn't worked

Drake
1 Mar, 2023, 18:15

what version of the Appwrite Flutter SDK do you have and is your app running on mobile or web?

godfather
1 Mar, 2023, 18:16

On flutter. And appwrite verison is 0.15.3 in server and flutter 6.0.0

godfather
1 Mar, 2023, 18:17
Drake
1 Mar, 2023, 18:18

ah ya...that version is older so you're affected by https://github.com/appwrite/sdk-for-flutter/issues/108

Drake
1 Mar, 2023, 18:19

in short, in your version of the SDK, the realtime stream is not closed when the web socket closes

godfather
1 Mar, 2023, 18:22

Ohh. So realtime was not active but it showed as active? As it didn't threw any error when I tried to close it? Any way to solve this issue or check it? So that I don't have to refresh data everytime my user brings app to foreground

Drake
1 Mar, 2023, 18:25

if you need to be notified about when the websocket closes, you can refer to how it was fixed in the PR for that issue I linked. You may need to fork the SDK or something to apply the patch to the SDK for your version

godfather
1 Mar, 2023, 18:30

Ok. These are the changes done to correct it? https://github.com/appwrite/sdk-for-flutter/commit/281e5d553fa7c25e6f5c9810cc73019648c99cca

Should, I directly change it in the src file of flutter and release it in the latest version, this will work right?

godfather
1 Mar, 2023, 18:33

Thanks, these are the only lines that need to be changed, can you please confirm?

TypeScript
}, onDone: () {
        for (var list in _channels.values) {
          for (var stream in list) {
            stream.close();
          }
        }
        _closeConnection();
      }, onError: (err, stack) {
        for (var list in _channels.values) {
          for (var stream in list) {
            stream.sink.addError(err, stack);
          }
        }
        if (_websok?.closeCode != null && _websok?.closeCode != 1008) {
          debugPrint("Reconnecting in one second.");
          Future.delayed(Duration(seconds: 1), _createSocket);
        }
      });
Drake
1 Mar, 2023, 18:34

scanning quickly, it looks okay...

godfather
1 Mar, 2023, 18:35

Ok. Thanks a lot πŸ™πŸ»

godfather
1 Mar, 2023, 19:45

I updated the package file and tried many things but neither : onError or onDone worked! I don't know whats the issue

TypeScript
liveDataSubscription!.stream.listen((data) {
      ///
      ///
    }, onDone: () {
      print('On Done');
      liveStreamBroken = true;
    }, onError: (error, st) {
      print('On Error - $error, $st');
      liveStreamBroken = true;
    });

It should have worked

Drake
1 Mar, 2023, 19:47

I would use the debugger and step through to see what's going on

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more