Reputation: 324
i tried using presence system but after tab is closed the script doesn't work. what to do to change status to offline .
const db = firebase.firestore();
const usersRef = db.collection('users'); // Get a reference to the Users collection;
// Set the specific user's online status to true
usersRef
.doc(userId)
.set({
online: true,
}, { merge: true});
Upvotes: 1
Views: 530
Reputation: 600126
Cloud Firestore doesn't come with built-in capabilities for tracking user presence. Since its wire protocol is connectionless, it's just not well suited for this.
If you need to track what users are online, I'd highly recommend starting with the Realtime Database for this. The API of this database has the capability to pre-set write operations that will happen when the server detects that the client is gone, and to detect on the client whether it is connected to the server or not. By combining these two, you can build a relatively simple presence system.
As said, since Firestore's wire protocol is connectionless, it doesn't have such a feature built-in. It is possible to combine Firestore and Realtime Database to create a presence system, but I'd recommend first learning how it works by using just the Realtime Database as shown in the links in the previous paragraph.
Upvotes: 2