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:
I know it takes that lot of time, because it is waiting the sockets to timeout to nominate the user has completely disconnected
I tried using goOffline
but with no luck, at least I wanted to make onDisconnect
event to be fired faster.
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.
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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…