Firebase 与firestore本地反应获取在线用户数

Firebase 与firestore本地反应获取在线用户数,firebase,react-native,google-cloud-firestore,Firebase,React Native,Google Cloud Firestore,我使用firebase/firestore在react native中构建应用程序。 我正在寻找一种方法来检查应用程序中的在线用户数,所以我找到了一种方法来处理 var userRef=new Firebase('https://.firebaseio.com/presence/'+用户ID); userRef.on('value',函数(快照){ if(snapshot.val()==真){ //用户在线,更新用户界面。 }否则{ //用户在snapshot.val()处注销-自epoch起的

我使用firebase/firestore在react native中构建应用程序。 我正在寻找一种方法来检查应用程序中的在线用户数,所以我找到了一种方法来处理

var userRef=new Firebase('https://.firebaseio.com/presence/'+用户ID);
userRef.on('value',函数(快照){
if(snapshot.val()==真){
//用户在线,更新用户界面。
}否则{
//用户在snapshot.val()处注销-自epoch起的秒数。
}
});
我正在寻找一种处理firestore和react native的方法。是否有任何实现我可以看到如何做到这一点

我找到了一种处理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();
});

当我在线时,它确实会用正确的uid更新状态集合,但当我断开与应用程序的连接时,它不会更新为脱机。我该怎么做呢?

当用户完全关闭应用程序时,它就会工作

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

我想一种方法是在Firestore文档中保留一个条目,例如“stats”,其中包含一个条目:
usersOnline:int
,您可以根据需要对其进行递增/递减。很好的方法..我会尝试这样做。酷,让我知道它是否适用于您。@ColinRicardo我更新了我的帖子,如果您能看到并提供帮助的话