I am listening to .info/connected to figure out if my device is online or not. If it is online it adds a child with the device id to the database.
_firebaseDatabase.reference().child('.info/connected').onValue.listen((event) {
if (event.snapshot.value == true) {
setDeviceOnline(userUid: uid, deviceUid: deviceUid)
}
});
I am using the onDisconnect callback on Firebase Realtime Database to remove this device id when the corresponding client disconnects.
Future<void> setDeviceOnline({String userUid, String deviceUid}) async {
DatabaseReference currentDeviceConnection = _firebaseDatabase.reference().child('users/$userUid/connections').push();
// Add this device to my connections list
await currentDeviceConnection.set(deviceUid);
// When I disconnect, remove this device
currentDeviceConnection.onDisconnect().remove();
}
Now:
- When the client explicitly disconnects, the
onDisconnectfires and removes the device id from the db. - When I turn on
airplane modetheonDisconnectfires after a short timeout and removes the device id from the db. - When I turn on
airplane modeand turn it back off before this timeout happens, theonDisconnectfires immediately when the client reconnects. Like it tells the db "I was offline"
The problem with point 3 is when the clients reconnects, onDisconnect fires and removes the present id from the database and due to the .info/connected listener which shows event.snapshot.value == true again, it immediately adds a new child with the device id to the database.
I think this works as intended. So I don't think this is a bug. But is there a way to prevent removing the child and adding a new one if there is still a corresponding child present in the db?
Once the server detects that the original connection is gone (either on its own, or because the client tells it about it), it executes the
onDisconnecthandler that you set for that connection.There is no way to prevent the removal of the previous connection node.
But since a new connection is established, your
setDeviceOnlineshould generate a newcurrentDeviceConnectionconnect ID again. In fact, that is precisely why the presence system in the Firebase documentation generates a new ID each time it reconnects. Is that not happening for you?