supabase / supabase-flutter

Flutter integration for Supabase. This package makes it simple for developers to build secure and scalable products.
https://supabase.com/
MIT License
735 stars 183 forks source link

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

Closed upits closed 1 month ago

upits commented 1 year ago

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();
DanMossa commented 1 year ago

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 commented 1 year ago

Thanks for the excellent support....

On 3/7/2023 12:28 PM, Daniel Mossaband wrote:

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

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

— Reply to this email directly, view it on GitHub https://github.com/supabase/supabase-flutter/issues/388#issuecomment-1458635727, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE6D63O3II5DNEIQ7JGCMTDW2546NANCNFSM6AAAAAAVSYXXPA. You are receiving this because you authored the thread.Message ID: @.***>

--

Jim @.***> UpITs: Unparalleled IT Solutions Phone: 918-527-1106 Web:http://www.UpITs.com

Vinzent03 commented 1 year ago

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

DanMossa commented 1 year ago

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 commented 1 year ago

Will check it out this weekend

On 3/16/2023 12:39 PM, Vinzent wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/supabase/supabase-flutter/issues/388#issuecomment-1472428293, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE6D63OWETMFVNQAXAQVQE3W4NF5NANCNFSM6AAAAAAVSYXXPA. You are receiving this because you authored the thread.Message ID: @.***>

--

Jim @.***> UpITs: Unparalleled IT Solutions Phone: 918-527-1106 Web:http://www.UpITs.com

dshukertjr commented 1 year ago

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 commented 1 year ago

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

upits commented 1 year ago

Will do it this weekend. Thanks for all the help.

⁣Jim Sills @.***> UpITs: Unparalleled IT Solutions Phone: 918-527-1106​

On Mar 17, 2023, 8:26 PM, at 8:26 PM, Tyler @.***> wrote:

I am not able to reproduct this quite yet, but @upits 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?

-- Reply to this email directly or view it on GitHub: https://github.com/supabase/supabase-flutter/issues/388#issuecomment-1474562761 You are receiving this because you were mentioned.

Message ID: @.***>

upits commented 1 year ago

Here was out test setup

Android:   Samsung Phone; Google Phone

Android Studio: Running an emulator

Windows Version: Windows 11

Web Version: Running in Chrome on Windows 11

All 3 Android versions (2 phone, and emulator) lost real time updates after 30 minutes of no Supabase activity

All other versions (Windows, Web) did not loose real time updates after 30 minutes of no Supabase activity

I added the code below to the Emulator (Android Studio) and no Event or Error printed

On 3/17/2023 8:25 PM, Tyler wrote:

I am not able to reproduct this quite yet, but @upits https://github.com/upits 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 https://github.com/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?

— Reply to this email directly, view it on GitHub https://github.com/supabase/supabase-flutter/issues/388#issuecomment-1474562761, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE6D63KDD4YUF27YPUCU2JDW4UFKJANCNFSM6AAAAAAVSYXXPA. You are receiving this because you were mentioned.Message ID: @.***>

--

Jim @.***> UpITs: Unparalleled IT Solutions Phone: 918-527-1106 Web:http://www.UpITs.com

dshukertjr commented 1 year ago

@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 commented 1 year ago

@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.

jaejune commented 1 year ago

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 commented 1 year ago

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

jaejune commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago
[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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 11 months ago

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 commented 1 month ago

any updates on this?

Vinzent03 commented 1 month ago

With #1019 being merged now, these issues should be solved now. You can try them by upgrading supabase_flutter to 2.7.0. If you still experience any issues, please create a new issue.