sdk-for-flutter icon indicating copy to clipboard operation
sdk-for-flutter copied to clipboard

🐛 Bug Report: Realtime does not reconnect when connection is lost

Open merabtenei opened this issue 2 years ago • 10 comments

👟 Reproduction steps

  • On android, create a subscription, check that it receives events correctly.
  • Now turn off wifi (and any other connectivity) for a few seconds.
  • Turn on wifi (or other connectivity) again.
  • Trigger some events
  • Now the subscription doesn't receive those event and the worst is there's no exception or anything triggered when the connection was lost

I'm using Flutter and below is a very simple app that will reproduce this behavior.

import 'package:appwrite/appwrite.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Appwrite Realtime Test',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Appwrite Realtime Test'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Client client = Client();

  late RealtimeSubscription subscription;

  @override
  void initState() {
    client
            .setEndpoint(
                'https://xxxxxxxx.xxx/v1') // Your Appwrite Endpoint
            .setProject('xxxxx') // Your project ID
            .setSelfSigned() // Use only on dev mode with a self-signed SSL cert
        ;
    Realtime realtime = Realtime(client);
    subscription =
        realtime.subscribe(['databases.xxxxx.collections.xxxxx.documents']);
    subscription.stream.listen((event) => print('Received event'),
        onDone: () => print('DONE'), onError: (error, st) => print('ERROR'));

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: StreamBuilder(
          stream: subscription.stream,
          builder: (context, snapshot) {
            if (snapshot.hasData && snapshot.data != null) {
              return Text(snapshot.data!.payload.toString());
            } else if (snapshot.hasError) {
              return Tooltip(
                message: snapshot.error.toString(),
                child: const Icon(Icons.error),
              );
            } else {
              return const CircularProgressIndicator();
            }
          },
        ),
      ),
    );
  }
}

👍 Expected behavior

One of two scenarios :

  • Websocket should reconnect automatically.
  • Or raise an exception or anything we can catch when the connection is lost so we can try to recreate the subscription manually

👎 Actual Behavior

  • No automatic reconnecting
  • No exception or error raised when deconnecting

🎲 Appwrite version

Version 1.1.x

💻 Operating system

Linux

🧱 Your Environment

No response

👀 Have you spent some time to check if this issue has been raised before?

  • [X] I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

merabtenei avatar Dec 05 '22 12:12 merabtenei