中处理用户的在线和离线状态

中处理用户的在线和离线状态

本文介绍了在 Firebase 中处理用户的在线和离线状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 webapp 中处理在线和离线状态.这样用户就可以看到谁在线,谁不在线.

I want to handle the online and offline status in my webapp.So that users can see who is online and who not.

我发现这个很棒的教程解释得很好,但我卡住了.

I found this awesome tutorial which explain it very good, but I am stuck.

https:///blog.campvanilla.com/firebase-firestore-guide-how-to-user-presence-online-offline-basics-66dc27f67802

我认为 cloud-functions 有问题,因为那里出现错误.

I thing there is a problem with the cloud-functions, because I got an error there.

此外,本教程来自 2017 年 12 月 15 日,我知道 cloud-functions 已更新,但我不知道如何更新代码.

Furthermore the tutorial is from Dec 15, 2017 and I know that the cloud-functions got updated but I don't know how to update the code.

文档链接:https://firebase.google.com/docs/functions/beta-v1-diff

有人可以看一下教程并可以帮助我吗?

Can someone take a look on the tutorial and can help me maybe?

云功能:

 const functions = require('firebase-functions');
 const Firestore = require('@google-cloud/firestore');
 const firestore = new Firestore();

 exports.onUserStatusChanged = functions.database
  .ref('/status/{userId}') // Reference to the Firebase RealTime database key
  .onUpdate((event, context) => {

    const usersRef = firestore.collection('/users'); // Create a reference to
    the Firestore Collection

    return event.before.ref.once('value')
      .then(statusSnapshot => snapShot.val()) // Get latest value from  the Firebase Realtime database
      .then(status => {
        // check if the value is 'offline'
        if (status === 'offline') {
          // Set the Firestore's document's online value to false
          usersRef
            .doc(event.params.userId)
            .set({
              online: false
            }, {
              merge: true
            });
        }
        return
      })
  });

推荐答案

Firestore 官方文档中有更新的教程(在 Cloud Firestore 中建立存在),说明如何设置 Firestore、实时数据库和 Cloud Functions.

There is an updated tutorial in the official Firestore documentation (Build presence in Cloud Firestore) that explains how to set up Firestore, Realtime Database, and Cloud Functions.

这篇关于在 Firebase 中处理用户的在线和离线状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:06