在Firebase中处理用户的在线和离线状态 [英] Handle users’ online and offline status in Firebase

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

问题描述

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

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
      })
  });

推荐答案

我将发布功能齐全的代码,以帮助那些像我一样坚持使用它的人.

I'm just going to post the fully functional code to help others who stuck with it like me.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

const firestore = functions.firestore;

exports.onUserStatusChange = functions.database
    .ref('/status/{userId}')
    .onUpdate((event, context) => {

        var db = admin.firestore();
        var fieldValue = require("firebase-admin").firestore.FieldValue;

        const usersRef = db.collection("users");
        var snapShot = event.after;

        return event.after.ref.once('value')
            .then(statusSnap => snapShot.val())
            .then(status => {
                if (status === 'offline'){
                    usersRef
                        .doc(context.params.userId)
                        .set({
                            online: false
                        }, {merge: true});

                }
                return null;
            })
});

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆