-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
base: main
Are you sure you want to change the base?
Conversation
// ... and write it to Firestore. | ||
await userStatusFirestoreRef.delete(); | ||
|
||
if ((await userDeviceCollectionRef.get()).empty) { |
There was a problem hiding this comment.
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+
presence-firestore/public/index.js
Outdated
@@ -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(); |
There was a problem hiding this comment.
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.
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.