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

Bug: Stream behaviour on connection lost and reconnect #674

Open
coolusaHD opened this issue Oct 18, 2023 · 8 comments
Open

Bug: Stream behaviour on connection lost and reconnect #674

coolusaHD opened this issue Oct 18, 2023 · 8 comments
Labels
bug Something isn't working realtime This issue or pull request is related to realtime

Comments

@coolusaHD
Copy link

Describe the bug
With the update to 1.2.3 , losing the connection doesn't trigger any error anymore. Also reconnecting to the internet doesn't change/trigger anything.

My current code (with v1.2.2)

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

class RealtimeGameService extends ChangeNotifier {
  RealtimeGameService({
    required this.client,
    required this.primaryKey,
    required this.tableName,
  }) {
    _init();
  }

  final SupabaseClient client;
  final String tableName;
  final List<String> primaryKey;

  List<Map<String, dynamic>> data = [];
  DateTime lastUpdateTime = DateTime.now();
  bool isConnected = false;
  bool isInitialLoading = true;

  StreamSubscription<List<Map<String, dynamic>>>? _streamSubscription;

  void _init() {
    _streamSubscription?.cancel(); // Cancel any existing subscription
    final liveDataStream =
        client.from(tableName).stream(primaryKey: primaryKey);

    _streamSubscription = liveDataStream.listen(
      (List<Map<String, dynamic>> newData) {
        print('works');
        print('Received data: $newData');
        _updateData(newData, true);
        if (isInitialLoading) {
          isInitialLoading = false; // Mark initial loading as complete
          notifyListeners();
        }
      },
      onError: (e) {
        print('Error during realtime subscription: $e');
        _updateData(data, false);
        if (isInitialLoading) {
          isInitialLoading = false; // Mark initial loading as complete
          notifyListeners();
        }
      },
      onDone: () {
        print('Realtime subscription done');
      },
    );
  }

  void _updateData(List<Map<String, dynamic>> newData, bool newIsConnected) {
    data = newData;
    lastUpdateTime = newIsConnected ? DateTime.now() : lastUpdateTime;
    isConnected = newIsConnected;
    notifyListeners();
  }

  void reload() {
    _init();
    isInitialLoading = true;
    notifyListeners();
  }

  @override
  void dispose() {
    _streamSubscription?.cancel();
    super.dispose();
  }
}

Expected behavior

  1. At least one single error should be thrown that the websocket lost connection
  2. It would be nice if the websocket reconnects it triggers the onData again

Version (please complete the following information):
????????? realtime_client 1.2.2
????????? supabase_flutter 1.10.18
??? ????????? supabase 1.11.5
??? ??? ????????? functions_client 1.3.2
??? ??? ????????? gotrue 1.12.4
??? ??? ????????? postgrest 1.5.1
??? ??? ????????? realtime_client...
??? ??? ????????? storage_client 1.5.3

@coolusaHD coolusaHD added the bug Something isn't working label Oct 18, 2023
@dshukertjr dshukertjr added the realtime This issue or pull request is related to realtime label Oct 18, 2023
@dshukertjr
Copy link
Member

Thanks for opening this @coolusaHD

I'm assuming the reason why you want to receive error within onError when listening to stream is to restart the stream when the connection is lost, correct? We are working on making the .stream() method more reliable so that it will self-reconnect if the realtime connection gets lost for any reason. If this is implemented, would you say there is no need to have an API to detect if the stream lost its connection?

@coolusaHD
Copy link
Author

coolusaHD commented Oct 27, 2023

@dshukertjr
In my implementation I use the onError for notifiy the user that he no longer recieves the latest data. To reconnect the user itself have to press a button to retry/ reinit the stream subscription.

So to answer your question: No I dont use it to automatically reconnect to the stream.

Therefore it would be nice if the stream would automatically reconnect and recall the onData callback but it would be also nice (and essential for me) that at least the onError gets triggered once when the stream lost the connection.

Thanks for asking🙌

@bigbenyayi
Copy link

bigbenyayi commented Oct 31, 2023

@dshukertjr Any ideas when the work on making the ".stream() method more reliable" is going to be finished? Or available for a first testing?

@coolusaHD
Copy link
Author

Any updates on this ? ✌️ @dshukertjr

@elliottetzkorn
Copy link
Contributor

Thanks for opening this @coolusaHD

I'm assuming the reason why you want to receive error within onError when listening to stream is to restart the stream when the connection is lost, correct? We are working on making the .stream() method more reliable so that it will self-reconnect if the realtime connection gets lost for any reason. If this is implemented, would you say there is no need to have an API to detect if the stream lost its connection?

Is there an ETA on this? Stream is so buggy right now it is really difficult to trust it.

@coolusaHD
Copy link
Author

@dshukertjr
I had some freetime and testet a bit. The change of the _onConnError function also prevents that the onError function of the stream gets the error.

But thats not the only problem. If the connection fails the heartbeat should also fail and close the stream after 2 missed heartbeats. But that doesn't trigger anything.

So from my perspective I see why the flooding of errors should be prevented but without any error how can I know if my stream is still connected to the source?

I really like to update to the latest version but my app cant live without that 😅

Is there any change to make the error flooding optional or even fix it that it returns at least one error?

Thanks in advance. ✌️

@maxfornacon
Copy link

maxfornacon commented Apr 29, 2024

I would also like to vote that at least an error message is displayed when the streams stop receiving data.

Supabase and the supabase_flutter package are such great products, but with faulty realtime support in certain cases, the package is simply not ready for production applications imho. This should at least be openly communicated to the customers.
If someone knows a reliable workaround for the stream issues, I would love to know about it. I couldn't find one myself over the last months and that's holding us back.

Any official statement would be highly appreciated. Otherwise, I love everything about Supabase and would recommend it anytime.

@fan123199
Copy link

Hope quick solve this issue. I also lost connect and do not receive error from stream.onError callback. It it difficult to fix it for the app developer.

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

6 participants