Guest User

Untitled

a guest
Dec 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. const firestoreDb = firebase.firestore();
  2. const oldRealTimeDb = firebase.database();
  3.  
  4. const usersRef = firestoreDb.collection('users'); // Get a reference to the Users collection;
  5. const onlineRef = oldRealTimeDb.ref('.info/connected'); // Get a reference to the list of connections
  6.  
  7. onlineRef.on('value', snapshot => {
  8.  
  9. oldRealTimeDb
  10. .ref(`/status/${userId}`)
  11. .onDisconnect() // Set up the disconnect hook
  12. .set('offline') // The value to be set for this key when the client disconnects
  13. .then(() => {
  14. // Set the Firestore User's online status to true
  15. usersRef
  16. .doc(userId)
  17. .set({
  18. online: true,
  19. }, { merge: true});
  20.  
  21. // Let's also create a key in our real-time database
  22. // The value is set to 'online'
  23. oldRealTimeDb.ref(`/status/${userId}`).set('online');
  24. });
  25.  
  26. });
Add Comment
Please, Sign In to add comment