Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realtime Channel seems to go to sleep after a while (~15 minutes) in Flutter App #388

Open
upits opened this issue Mar 7, 2023 · 26 comments
Labels
bug Something isn't working realtime This issue or pull request is related to realtime

Comments

@upits
Copy link

upits commented Mar 7, 2023

Flutter app subscribed to Realtime channel stops receiving events after a while (~15 minutes). The app has to be closed and reopened then events will be received again (for a while ~15 minutes).
Subscription code:

supabaseClient.channel('public:tbl_name').on(
  RealtimeListenTypes.postgresChanges,
    ChannelFilter(
        event: 'UPDATE',
        schema: 'public',
        table: 'tbl_entry_queue'), (payload, [ref]) {
    payload.forEach((key, value) {
        if (key == 'new') {
            value.forEach((key2, value2) {
               // Processing code for "Payload"
           });
      }
   });
}).subscribe();
@upits upits added the bug Something isn't working label Mar 7, 2023
@DanMossa
Copy link
Collaborator

DanMossa commented Mar 7, 2023

This happens due to Supabase closing a connection if it's unused within X minutes.

@Vinzent03 is working on a PR that will allow us to capture the Exception that occurs and reconnect like that.

@upits
Copy link
Author

upits commented Mar 7, 2023 via email

@Vinzent03
Copy link
Collaborator

Is this issue with realtime_client: ^1.0.3 resolved? Run (flutter pub upgrade realtime_client)

@DanMossa
Copy link
Collaborator

I'm not sure if this issue would be due to Supabase cutting off the connection after being idle. But now that we can catch the error if/when it happens, we can decide how to handle it on a case by case basis.

Like for me, I want it so that if the app goes in the background the connection stops, and it starts again once the app is foregrounded.

@upits
Copy link
Author

upits commented Mar 17, 2023 via email

@dshukertjr
Copy link
Member

dshukertjr commented Mar 18, 2023

I am not able to reproduct this quite yet, but @upits whenever you have the chance to try it out, would you be able to add callback to your .subscribe method like this to see if the connection is erroring or closing?

.subscribe((event, [error]) {
      print(event);
      print(error);
    });

@DanMossa
Realtime SDK not reconnecting after going into background mode and coming back seems like a separate issue, but might be a good issue to tackle! Would you want to open an issue for it?

@DanMossa
Copy link
Collaborator

@dshukertjr I actually can't get it to reproduce with the new fixes that @Vinzent03 added!

@upits
Copy link
Author

upits commented Mar 18, 2023 via email

@upits
Copy link
Author

upits commented Mar 21, 2023 via email

@dshukertjr
Copy link
Member

@upits
Thanks for the debugging details. This is extremely helpful. I will try to reproduce this issue and see if I can come up with a fix for it.

@dshukertjr
Copy link
Member

@upits
I was unable to leave my Android device connected to the realtime listener with no activities on the Supabase side for well over 30 minutes but I was not able to reproduce the issue you are experiencing. If you could provide a public GitHub repo with the minimum code to reproduce your issue, that would be very much appreciated.

@dshukertjr dshukertjr added the realtime This issue or pull request is related to realtime label May 12, 2023
@jaejune
Copy link

jaejune commented May 25, 2023

same issue happened in iOS

  return db
        .stream(primaryKey: ['room_id'])
        .eq('uid', uid)
        .order('updated_at')
        .limit(1);

I used the stream method, and if I stayed in the pause state for about 30 minutes, I felt like socket was automatically disconnected

@bdlukaa
Copy link
Collaborator

bdlukaa commented May 25, 2023

Maybe a debounce timer should be added to check if the channels are still active?

@jaejune
Copy link

jaejune commented Jun 15, 2023

@dshukertjr I caught an error in the supabase realtime with firebase crashlytics Do you happen to know the cause?

Fatal Exception: FlutterError
0  ???                            0x0 SupabaseStreamBuilder._addException + 442 (supabase_stream_builder.dart:442)
1  ???                            0x0 SupabaseStreamBuilder._getStreamData.<fn> + 348 (supabase_stream_builder.dart:348)
2  ???                            0x0 RealtimeChannel.subscribe.<fn> + 200 (realtime_channel.dart:200)
3  ???                            0x0 RealtimeChannel.onError.<fn> + 329 (realtime_channel.dart:329)
4  ???                            0x0 RealtimeChannel.trigger + 566 (realtime_channel.dart:566)
5  ???                            0x0 RealtimeClient._triggerChanError + 395 (realtime_client.dart:395)
6  ???                            0x0 RealtimeClient._onConnClose + 376 (realtime_client.dart:376)
7  ???                            0x0 RealtimeClient.connect.<fn> + 137 (realtime_client.dart:137)

@dshukertjr
Copy link
Member

@bdlukaa
Do you mean to add something to realtime, or do you mean to add it to supabase-dart in the stream builder class?

@jaejune
Thanks for sharing this! Were there any error messages, or is this the entire error?

@jaejune
Copy link

jaejune commented Jun 16, 2023

@dshukertjr
FlutterError - null
I'm sorry, but strangely, the error message is null and this is entire error
This error is the most common error in my app
I'll collect the logs and let you know in detail later

@dshukertjr
Copy link
Member

@jaejune
Thanks for the info. It seems like the error is not coming through on the client for some reason. I will look into it.

@andreystavitsky
Copy link

[ChatLatestMessagesSupabaseRemoteSource] null
[ChatLatestMessagesSupabaseRemoteSource] #0      SupabaseStreamBuilder._addException
supabase_stream_builder.dart:442
#1      SupabaseStreamBuilder._getStreamData.<anonymous closure>
supabase_stream_builder.dart:348
#2      RealtimeChannel.subscribe.<anonymous closure>
realtime_channel.dart:200
#3      RealtimeChannel.onError.<anonymous closure>
realtime_channel.dart:329
#4      RealtimeChannel.trigger
realtime_channel.dart:566
#5      RealtimeClient._triggerChanError
realtime_client.dart:395
#6      RealtimeClient._onConnClose
realtime_client.dart:376
#7      RealtimeClient.connect.<anonymous closure>
realtime_client.dart:137
#8      _RootZone.runGuarded (dart:async/zone.dart:1582:10)
#9      _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
#10     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
#11     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
#12     _ForwardingStream._handleDone (dart:async/stream_pipe.dart:99:10)
#13     _ForwardingStreamSubscription._handleDone (dart:async/stream_pipe.dart:161:13)
#14     _RootZone.runGuarded (dart:async/zone.dart:1582:10)
#15     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
#16     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
#17     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
#18     _SyncStreamControllerDispatch._sendDone (dart:async/stream_controller.dart:784:19)
#19     _StreamController._closeUnchecked (dart:async/stream_controller.dart:639:7)
#20     _StreamController.close (dart:async/stream_controller.dart:632:5)
#21     _RootZone.run (dart:async/zone.dart:1655:54)
#22     _FutureListener.handleWhenComplete (dart:async/future_impl.dart:190:18)
#23     Future._propagateToListeners.handleWhenCompleteCallback (dart:async/future_impl.dart:754:39)
#24     Future._propagateToListeners (dart:async/future_impl.dart:810:11)
#25     Future._completeWithValue (dart:async/future_impl.dart:584:5)
#26     Future._asyncCompleteWithValue.<anonymous closure> (dart:async/future_impl.dart:657:7)
#27     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#28     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

@JasonChiu-dev
Copy link

same issue happened in iOS

  return db
        .stream(primaryKey: ['room_id'])
        .eq('uid', uid)
        .order('updated_at')
        .limit(1);

I used the stream method, and if I stayed in the pause state for about 30 minutes, I felt like socket was automatically disconnected

I recently experienced the same problem, even though the other app, which has been running for almost two years using Supabase and the supabase_flutter package with Flutter StreamBuilder and Supabase realtime, still experiences the same issue now. I have not made any changes to the app recently. The problem occurs on iOS and web platforms when using Flutter StreamBuilder with the supabase_flutter package and realtime. The error occurred in StreamBuilder, and I received the error when I checked if (snapshot.hasError) in StreamBuilder, but snapshots.error was empty. Is there anyone in the Supabase community or on the Supabase tech team who can help resolve this problem? It is a serious problem that is disrupting my customers and me. I would appreciate your help.

@Omicrxn
Copy link

Omicrxn commented Aug 25, 2023

@Vinzent03 Any updates on this? Still happens on v.1.10.14 it is a huge problem since the data is not reflected on the app past ~15 mins or after computer sleeps.

@dshukertjr
Copy link
Member

Anyone experiencing this issue, would you please provide as detailed as possible steps to reproduce this error? I have tested the realtime connection on iOS, Android, and web, and I am able to receive real-time events for multiple hours without any issues.

If anyone could create a minimal Flutter app that can reliably reproduce this issue and share its GitHub repo, that would help us a lot at finding the cause and fixing this issue.

@maxfornacon
Copy link

maxfornacon commented Sep 4, 2023

I created a minimal Flutter (desktop/MacOS) app to reproduce my issue: #579. Since it is also a realtime issue this could be related. Maybe you can have a look at it.

@dshukertjr
Copy link
Member

@JasonChiu-dev
I think your issue is fixed in the most recent version available that was just released.

For other folks experiencing real-time stream closing after around 15 min or so, I am actually still unable to reproduce the issue. If you can reliably reproduce the issue, I would really appreciate it if you could let me know so that we could work together to find the root cause of it.

@Mangielijah
Copy link

Hi @dshukertjr 👋🏾
I took some time out to build a basic todo app on ZAPP where you can create todo's to reproduce the issue
Here is the web app link => https://zd9k06kxd9l0.zapp.page/#/
and here is the project link => https://zapp.run/edit/riverpod-todo-app-zd9k06kxd9l0?entry=lib/main.dart&file=lib/todo.dart

Step 1 => Open Both the web app link and Zapp Project Link
Step 2 => Create a Todo on both (web app link and Zapp link) to ensure its realtime
Step 3 => 😅 Open Youtube and watch any 20 - 30 mins supabase tutorial
Step 4 => Come back and check you should see the screenshot below

Screenshot 2023-09-15 at 8 18 13 PM

@KannugoPrithvi
Copy link

KannugoPrithvi commented Dec 6, 2023

Hi @dshukertjr ,

I'm still facing this issue as well. I'm currently doing a POC in a seperate branch where Im migrating my app from firebase to supabase. This is one bug that is seriously bothering me which was actually very seamless in firebase. Currently using the latest supabase flutter version [1.10.25]. Is there any update on the fix for this issue ? For more context, this issue is happening in both IOS simulator and Android physical device.

@Sliman-Baghouri
Copy link

any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working realtime This issue or pull request is related to realtime
Projects
None yet
Development

No branches or pull requests