Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed onDisconnect logic to use deviceId #1010

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

maneesht
Copy link
Collaborator

@maneesht maneesht commented Aug 29, 2022

The example originally used uid documents to determine whether a user is online or not. This is not best practice because you may have an onDisconnect that sets the user as offline, and if the same user open two different tabs, close one tab, and the user would show up as offline, even though one tab is still open.

This PR adds a devices collection and then adds a new document that represents the device's ID, and onDisconnect, will set the device's status to offline. The coThe function will check that change and remove the device from the Firestore user's devices collection and check if there are no devices in the collection. If that is the case, then the user itself will be removed from the user collection.

The client will then check Firestore for how many users are in the users collection.

// ... and write it to Firestore.
await userStatusFirestoreRef.delete();

if ((await userDeviceCollectionRef.get()).empty) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async/await syntax targets ES2016+, which is inconsistent with the rest of the file. Looking for whether ES5 is the target, or ES2016+

@@ -21,7 +21,8 @@ function rtdb_presence() {

// Create a reference to this user's specific status node.
// This is where we will store data about being online/offline.
var userStatusDatabaseRef = firebase.database().ref('/status/' + uid);
// `push()` will add a new key to the user's path, acting as a deviceId
var deviceStatusDatabaseRef = firebase.database().ref('/status/' + uid + '/devices').push();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sessions or connections is probably more accurate than devices, since the id isn't based on the device id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants