Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

flutter - Firebase OnDisconnect Event

so the question is about onDisconnect in firebase, I am implementing a chatapp like whatsapp in flutter and using Firebase, stuck at this point the onDisconnect event takes 2-3 mins to fire when you have no internet access:

  1. I know it takes that lot of time, because it is waiting the sockets to timeout to nominate the user has completely disconnected

  2. I tried using goOffline but with no luck, at least I wanted to make onDisconnect event to be fired faster.

  3. I have a workaround by making a Timer in flutter and every X period I update with timestamp indicating that this user is alive, but the question is who will trigger the offline status for this user when he is offline? a cloud function, or another online connected user? Since, this user didn't respond in X period so we need to update his status to offline.

  4. Is there any workaround for this issue?

Thanks in advance.

sample code I am using: in the initState() I am listening to /.info/connected:

 FirebaseDatabase.instance
        .reference()
        .child('/.info/connected')
        .onValue
        .listen((data) {
      print('bool value');
      print(data.snapshot.value);
      if (data.snapshot.value == false) {
        //user dc
        print('user disconnected');
        FirebaseDatabase.instance.goOffline().then((value) => print("offline"));
      } else {
        print('user reconnected');
        FirebaseDatabase.instance.goOnline().then((value) => print("user online again"));
        FirebaseDatabase.instance.reference().child('uid').onDisconnect().set({'status': 'offline'}).then((value) => print('here I am '));
      }
    });


See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...