Back

[SOLVED] Realtime Connections in Flutter do not get disposed!

  • 0
  • Flutter
  • Realtime
Arrow
14 Sep, 2023, 21:55

Hello, In Flutter, after disposing my listeners, the console still shows the connections, what might be wrong?

TL;DR
The user was experiencing an issue where realtime connections in Flutter were not getting disposed. The user found a solution, which involved properly closing the realtime subscription using the `realtimeSub?.close();` method. The issue was marked as solved, but the user noted that it may still be a concern if they have a large number of simultaneous users connecting.
Arrow
14 Sep, 2023, 21:57
TypeScript
  @override
  Future<RealtimeSubscription> getDoctorModelRealtimeSubscription(
      DoctorModel value) async {
    final rt = realtime.subscribe([
      'databases.$DatabaseId.collections.$DoctorsCollectionId.documents'
    ]);
    return rt;
  }

....

  StreamSubscription? _sub;

  Future<void> _startRealtimeSubscription() async {
    if (state.documentId.isEmpty || !mounted) return;

    final realtimeSubscription =
        await AsyncValue.guard(() => getDoctorModelRealtimeSubscription(state));

    if (realtimeSubscription is AsyncData<RealtimeSubscription>) {
      _sub = realtimeSubscription.value.stream.listen(_onRealtimeMessage);
    }
  }

 @override
  void dispose() {
    _sub?.cancel();
    super.dispose();
  }
D5
14 Sep, 2023, 21:59

Are you using Appwrite cloud?

Arrow
14 Sep, 2023, 21:59

self hosting

D5
14 Sep, 2023, 21:59

What version?

Arrow
14 Sep, 2023, 21:59

1.3.7

D5
14 Sep, 2023, 21:59

Same version as I'm using πŸ‘€

D5
14 Sep, 2023, 22:00

I had something similar in flutter. Connection was cancelled but it remained

Arrow
14 Sep, 2023, 22:02

It's a weird behavior, I'm certain of my dispose methods but the connection is still there

D5
14 Sep, 2023, 22:08

If you close the app completely it stops?

Arrow
14 Sep, 2023, 22:14

a hot restart resets things yes

Arrow
14 Sep, 2023, 22:17

@D5 have you spent time around this issue?

Arrow
14 Sep, 2023, 22:20

Realtime Connections in Flutter do not get disposed!

Arrow
14 Sep, 2023, 22:30

I just found out thru some experiment that the following solves the issue

TypeScript
...
realtimeSub = realtimeSubscription.value;
...
realtimeSub?.close();
Arrow
14 Sep, 2023, 22:30

@D5 see if this can also solve yours.

D5
14 Sep, 2023, 22:31

Since up to 40 users connected through web simultaneously (the js app worked without issues) and there was only 1 flutter app user, sincerely I ignored the issue πŸ˜†

D5
14 Sep, 2023, 22:33

Seems it solves too

Arrow
14 Sep, 2023, 22:34

this one with leaky streams was already in production, I had to find a solution

Arrow
14 Sep, 2023, 22:35

but its fine as long as we dont have a million users simultaneously connecting lol

D5
14 Sep, 2023, 22:35

I'm marking this as solved then

D5
14 Sep, 2023, 22:35

[SOLVED] Realtime Connections in Flutter do not get disposed!

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