react-native-firebase
react-native-firebase copied to clipboard
[🔥] onDisconnect not doing anything
Issue
I am trying to build a presence system following the docs. very simply I have the following:
const amOnline = database().ref(`.info/connected`)
const presenceRef = database().ref(`users/${user.uid}/watchers`)
amOnline.on('value', (snapshot) => {
if (snapshot.val()) {
console.log('connected val: ', snapshot.val())
presenceRef.onDisconnect().update({ isOnline: false })
} else {
console.log('COMING OFFLINE')
}
});
I can clearly see the "connected val: true" in my console.log; but the onDisconnect() update is NEVER called (trying to force close the app, close properly, close internet, etc) Also, the else "coming offline" log is also never triggered.
I am using firebase for years, everything else is working fine, so it's really just the onDisconnect that seems void. I'm trying on an emulator (again, every other firebase implementation, database accesses / updates / etc work fine)
- Platform that you're experiencing the issue on:
- [ ] iOS
- [x ] Android
- [ ] iOS but have not tested behavior on Android
- [ x] Android but have not tested behavior on iOS
- [ ] Both
react-native-firebaseversion you're using that has this issue:- 14.9.1
Firebasemodule(s) you're using that has the issue:- firebase database
- Are you using
TypeScript?- No
I observed a lot of flaky behavior myself in the .info/connected node, you can see I had to xit (vs it, in our testing library mocha's terminology xit means cross that test out temporarily)
https://github.com/invertase/react-native-firebase/blob/main/packages/database/e2e/internal/connected.e2e.js
I had success with simply running onDisconnect handlers though, perhaps that is a sufficient building block to build the presence system, if the style is adapted somewhat? Here's the enabled test that passes every CI run, nice and stable:
https://github.com/invertase/react-native-firebase/blob/ee740f80501fdd2ea5184515c4840340fca55e63/packages/database/e2e/onDisconnect/set.e2e.js#L67-L78
We document doing a presence system based on onDisconnect though, so I think it will be sufficient? https://rnfirebase.io/database/presence-detection
Thank you @mikehardy for answering.
I tried with your suggestion, using onDisconnect() on my presence ref as follow:
const checkPresence = useCallback(async() => {
const presenceRef = database().ref(`users/${user.uid}/watchers`)
setTimeout(() => {
console.log('GOING OFFLINE')
database().goOffline()
}, 1500)
await presenceRef.onDisconnect().update({ isOnline: false })}
}, [])
But unfortunately no success yet. When I restart my emulator, or when the timeout triggers the "goOffline()", the users/uid/watchers/isOnline doesn't get updated.
Did I misunderstand something from your answer?
Also, do we really need the await onDisconnect?
Your code differs from our docs. It might be valid or it might not.
Please make an app.js that uses our example code exactly as a minimal viable reproduction. Make a button that goes offline and online and a text in a view that prints status.
Sorry I should have mentioned I did try the code from your doc as follow:
const reference = database().ref(`/users/${user.uid}/watchers/isOnline`);
reference.set(true).then(() => console.log('Online presence set'));
// Remove the node whenever the client disconnects
reference
.onDisconnect()
.remove()
.then(() => console.log('On disconnect function configured.'));
set(true) gets triggered as expected; but the onDisconnect() never does; it litterally does nothing and never gets triggered, even with a button and goOffline()
it should be working on an emulator as well, right?
Our e2e test harness runs on emulators, you can see our harness setup in .github/workflows here, it's all open, every single line of source and infrastructure for the whole project
You keep showing snippets - https://stackoverflow.com/help/mcve
Please make an app.js that uses our example code exactly as a minimal viable reproduction. Make a button that goes offline and online and a text in a view that prints status.
Also you hacked up our issue template and did not provide all the info we ask for, perhaps forgivable but it decreases quality of support. https://stackoverflow.com/help/how-to-ask
If there were an app.js I could perhaps drop it in our test project and examine. But I can't really help when just tossing snippets around with assertions of not working and no full spec of test environment or versions etc.
Hello 👋, to help manage issues we automatically close stale issues.
This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?
This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.