且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

firebase上的用户数量(未注销)是否等于同时连接的数量?

更新时间:2023-12-02 13:38:40

当您启动使用Firebase数据库的应用程序时,它会创建与Firebase服务器的持续连接。这可以算作后端的一个活动连接。



连接可以在下列情况下破坏:


  1. 用户没有写入任何数据,并且几分钟内没有任何活动的侦听器(目前只有5分钟,但可以更改)。您打电话给
  2. goOffline()在您的代码中

无论应用程序处于前台还是背景与Firebase SDK无关。但是您可以使用Android生命周期事件(如 onPause())来检测这种转换并删除您的侦听器(以获得条件1)。 b
$ b

另外:当您的应用程序停滞时,Android操作系统可能随时关闭从客户端到Firebase后端的连接。最后一个选项(正如Firebaser的同事Doug在评论中指出的那样)是启用自动资源管理。如果启用自动资源管理,则当应用程序进入后台时,客户端本质上会调用 goOffline(),并且 goOnline()$ c $当它回到前台时。


I have an android app developed and I am expecting more than a 100 users.The app does have a logout button but I doubt any user would logout,they would instead push it to background. So my question is if I have 150 users ( logged in ,but in background) mean that I have 150 simultaneous connections ? I did read a few questions on simultaneous connections , but can someone be more accurate on what is a concurrent connection. 150 users - logged in (background) - open app at different times - ? If this is going to exceed the free plan on firebase , how do i prevent it?.I use email/password for login.

When you start an app that uses the Firebase Database, it creates a persistent connection to the Firebase server. This counts as one active connection on the back-end.

The connection can be broken when:

  1. the user hasn't written any data and doesn't have any active listeners for a few minutes (currently 5 minutes, but that could change).
  2. you call goOffline() in your code

Whether the app is in the foreground or background is irrelevant to the Firebase SDK. But you could use Android life-cycle events (such as onPause()) to detect such transitions and remove your listeners (to get to condition 1).

Also: when your app is backgrounded, the Android operating system might close the connection from the client to the Firebase back-end at any time. When it does this depends on the version and flavor of Android your device has.

A final option (as fellow Firebaser Doug pointed out in the comments) is to enable automatic resource management. If you enable automatic resource management, the client will essentially call goOffline() when the app goes into the background and goOnline() when it comes back to the foreground.