Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const firestoreDb = firebase.firestore();
- const oldRealTimeDb = firebase.database();
- const usersRef = firestoreDb.collection('users'); // Get a reference to the Users collection;
- const onlineRef = oldRealTimeDb.ref('.info/connected'); // Get a reference to the list of connections
- onlineRef.on('value', snapshot => {
- oldRealTimeDb
- .ref(`/status/${userId}`)
- .onDisconnect() // Set up the disconnect hook
- .set('offline') // The value to be set for this key when the client disconnects
- .then(() => {
- // Set the Firestore User's online status to true
- usersRef
- .doc(userId)
- .set({
- online: true,
- }, { merge: true});
- // Let's also create a key in our real-time database
- // The value is set to 'online'
- oldRealTimeDb.ref(`/status/${userId}`).set('online');
- });
- });
Add Comment
Please, Sign In to add comment