I've got a near full-functioning realtime database presence system as described here but I am encountering a few issues. It seems that people are remaining online even after having disconnected a while ago. I'm not sure why but I've opened up my security rules to unauthenticated requests temporarily to no avail. It could be due the bug described here.
If the issue is the ladder, what would be the proper JavaScript implementation to avoid this issue? Is it a good solution to recreate the onDisconnect listener every 60 minutes? For reference, the code I'm using is shown below:
export function selfPresence(byUpdate) {
onValue(ref(rtdb, '.info/connected'), (snap) => {
isDBConnected = snap.val();
if (snap.val() === true) {
presencecon = push(ref(rtdb, `users/${user.uid}/connections`)); // Create new presence thing.
onDisconnect(presencecon).remove();
set(presencecon, true);
onDisconnect(ref(rtdb, `users/${user.uid}/lastOnline`)).set(serverTimestamp());
onDisconnect(ref(rtdb, `users/${user.uid}/currentlyListening`)).remove();
}
});
}
MY SOLUTION
onValue(ref(rtdb, '.info/connected'), (snap) => {
isDBConnected = snap.val();
if (snap.val()) {
onDisconnect(presencecon).set(false);
presencecon = ref(rtdb, `users/${user.uid}/online`);
isDBConnected = snap.val();
}
else {
remove(presencecon);
}
}
The below code updates a boolean at a specific path so that if/when the issue DOES happen, the next time the user is online, it will overwrite the failure with a new online listener.
onValue(ref(rtdb, '.info/connected'), (snap) => {
isDBConnected = snap.val();
if (snap.val()) {
onDisconnect(presencecon).set(false);
presencecon = ref(rtdb, `users/${user.uid}/online`);
isDBConnected = snap.val();
}
else {
remove(presencecon);
}
}