Skip to content

Bug: Stream behaviour on connection lost and reconnectΒ #674

@coolusaHD

Description

@coolusaHD

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingrealtimeThis issue or pull request is related to realtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions