Firestore(Firebase)如何计算活动/在线用户总数 [英] Firestore (Firebase) How to count total active/online users

查看:57
本文介绍了Firestore(Firebase)如何计算活动/在线用户总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将创建一个具有以下某些功能的应用程序:

I am going to create an app with some features below:

  • 无需注册或登录即可使用该应用程序

  • No need to signup or login to use the app

每天,我的管理员都会将一个10分钟长(或更少)的音频文件上传到后端,并设置音频可供所有用户使用的时间.例如:上午10点

Everyday, my admin will upload a 10 mins long (or less) audio file to backend and setup the time when the audio will be available for all users. E.g: 10am

在设置时间(9.55am)之前5分钟,该应用将开始计算正在打开该应用并等待播放音频文件的用户总数.

5 mins before the setup time (9.55am), the app will start to count total number of users who are opening the app and waiting for to play the audio file.

活跃/在线用户的数量将是实时的.

我尝试过的事情:

  • 每次用户打开音频屏幕时,应用程序将使用FirebaseAuth以匿名身份登录.当用户离开此屏幕时,请使用FirebaseAuth注销.但是这种方法不起作用,因为当用户注销时,用户表上仍然存在匿名用户

我的问题:

  1. 有了这些功能,我可以使用firebase/firestore来实现吗?

  1. With those features, can I use firebase/firestore to implement?

计算活动用户总数的最佳方法是什么?

What is the best approach to count total active users?

推荐答案

您可以使用 FirebaseAuth 将值写入数据库,如果auth返回某人正在使用该应用程序,请编写一个参考并继续添加用户.

You can use the FirebaseAuth to write a value into the database, if the auth returns that someone is using the app, write a reference and keep adding users on it.

我的意思是,如果有一个用户在线,则 FirebaseAuth 会将值1写入名为 usersonline 的引用,如果另一个用户加入,它将是 usersonline + 1 等,然后您可以附加 onChildEventListener 并使用 onChildChanged 在线实时更新用户,因此,每次孩子更改时,(根据用户的增加),它将在线显示用户.

I mean, if there is an user online, FirebaseAuth will write to a reference called usersonline a value of 1 , if another user joins it's going to be usersonline + 1 and so on, and then you can attach an onChildEventListener and update in realtime the users online with onChildChanged, so, each time that child changes (incrementing depending the users) it will be showing the users online.

要使用户在线减少,请在他们注销并且 AuthListener 不再看到已登录的用户后,您可以在每次用户注销时仅减少1,因此,如果用户登录退出,AuthListener将触发,并且每次发生这种情况时,您都可以使用 usersonline-1 修改您的孩子.

To decrement the users online, after they logout and an AuthListener is no longer seeing the logged in user, you can decrement just by 1 each time an user logs out, so, if an user logs out, the AuthListener will fire and you can modify your child with usersonline - 1 each time this happens.

例如,当用户失去连接时,Firebase设法将数据写入数据库.

For example, Firebase manages to write data to the database when an user loses their connection.

// Write a string when this client loses connection
presenceRef.onDisconnect().setValue("I disconnected!");

您可以在离线功能中对其进行检查:

You can check it in the offline capabilities:

https://firebase.google.com/docs/database/android/offline-capabilities

因此,您无需编写(我已断开连接!"),而不是将usersonline ref递减1,因此您将像实时在线用户一样添加人员并将其从该孩子中删除.

So instead of writing (" i disconnected!") what you want to achieve is to decrement the usersonline ref by 1 , so you will be adding people and removing them from that child like realtime online users.

该文档的这段代码对于实现您想要的内容确实非常有用

This snippet from that doc is really good to achieve what you are looking for

DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
    boolean connected = snapshot.getValue(Boolean.class);
    if (connected) {
      System.out.println("connected"); // tell your usersonline ref to increment by 1
    } else {
      System.out.println("not connected"); //then if the user is not longer connected just decrement your usersonline by 1
    }
  }

  @Override
  public void onCancelled(DatabaseError error) {
    System.err.println("Listener was cancelled");
  }
});

就同时连接而言,这是针对Firebase实时数据库的:

In terms of simultaneous connections, this is for Firebase realtime database:

Spark计划限制为100,无法提高.火焰与烈焰计划对每个数据库的同时连接数限制为100,000.如果您需要同时进行100,000个以上的连接,请联系Firebase支持.

The Spark plan limit is 100 and cannot be raised. The Flame and Blaze plans have a limit of 100,000 simultaneous connections per database. If you need more than 100,000 simultaneous connections, contact Firebase support.

https://firebase.google.com/docs/firestore/quotas 对于Firestore,目前同时进行连接的限制为100k,这仅是测试阶段.

https://firebase.google.com/docs/firestore/quotas for Firestore, 100k is the limit for simultaneous connections at the moment, this is just for the beta period.

这篇关于Firestore(Firebase)如何计算活动/在线用户总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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