0
votes

I build app in react native with firebase/firestore. I'm looking a way to check the count of users online in app, so I found a way to do with this tutorial

var userRef = new Firebase('https://<demo>.firebaseio.com/presence/' + userid);
userRef.on('value', function(snapshot) {
  if (snapshot.val() === true) {
    // User is online, update UI.
  } else {
    // User logged off at snapshot.val() - seconds since epoch.
  }
});

I'm looking a way to do with firestore and react native. is there any implementation i can see how do that?

I found this way to do with firestore

import { Platform } from 'react-native';
import firebase from 'react-native-firebase';


function rtdb_and_local_fs_presence() {
    // [START rtdb_and_local_fs_presence]
    // [START_EXCLUDE]
    var uid = firebase.auth().currentUser.uid;
    console.log('uid',uid)
    var userStatusDatabaseRef = firebase.database().ref('status/' + uid);

    var isOfflineForDatabase = {
        state: 'offline',
        last_changed: firebase.database.ServerValue.TIMESTAMP,
    };

    var isOnlineForDatabase = {
        state: 'online',
        last_changed: firebase.database.ServerValue.TIMESTAMP,
    };

    // [END_EXCLUDE]
    var userStatusFirestoreRef = firebase.firestore().doc('status/' + uid);

    // Firestore uses a different server timestamp value, so we'll 
    // create two more constants for Firestore state.
    var isOfflineForFirestore = {
        state: 'offline',
        last_changed: firebase.firestore.FieldValue.serverTimestamp(),
    };

    var isOnlineForFirestore = {
        state: 'online',
        last_changed: firebase.firestore.FieldValue.serverTimestamp(),
    };

    firebase.database().ref('.info/connected').on('value', function(snapshot) {
        if (snapshot.val() == false) {
            // Instead of simply returning, we'll also set Firestore's state
            // to 'offline'. This ensures that our Firestore cache is aware
            // of the switch to 'offline.'
            userStatusFirestoreRef.set(isOfflineForFirestore);
            return;
        };

        userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() {
            userStatusDatabaseRef.set(isOnlineForDatabase);

            // We'll also add Firestore set here for when we come online.
            userStatusFirestoreRef.set(isOnlineForFirestore);
        });
    });
    // [END rtdb_and_local_fs_presence]
}

function fs_listen() {
    var uid = firebase.auth().currentUser.uid;
    var userStatusFirestoreRef = firebase.firestore().doc('status/' + uid);

    // [START fs_onsnapshot]
    userStatusFirestoreRef.onSnapshot(function(doc) {
        var isOnline = doc.data().state == 'online';
        // ... use isOnline
    });
}


firebase.auth().signInAnonymouslyAndRetrieveData().then((user) => {
    rtdb_and_local_fs_presence();
    fs_listen();
});

it really update the status collection with the right uid when I'm online, but when I disconnect from app, it not update to offline. how can I do that?

1
I suppose one way would be to keep an entry in a Firestore document e.g. 'stats' which holds one entry: usersOnline: int, which you would just increment / decrement as necessary.Colin Ricardo
nice way..I will try do that.Manspof
Cool, let me know if it works for you.Colin Ricardo
@ColinRicardo I update my post, if you can see and help pleaseManspof

1 Answers

0
votes

It will be working when user closes the app completly.

onStatusOffline(user){
     firebase.database().ref(`users/${user.uid}`) 
     .onDisconnect()
     .update({
         online: false,
     });
}