有没有办法在不向客户端公开当前计数的情况下增加Firebase中的计数? [英] Is there a way to increment a count in Firebase without exposing current count to the client?

查看:61
本文介绍了有没有办法在不向客户端公开当前计数的情况下增加Firebase中的计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Firebase中增加一个计数,但是不将当前计数暴露给客户端.

I would like to increment a count in Firebase, but without exposing the the current count to client.

我已经阅读了许多有关在Firebase中使用事务递增计数器的有用答案,但是所有这些都假定客户端对计数器元素具​​有读取"访问权限. 如果客户端不具有对计数的读取权限,但是我想根据用户的操作来增加值,该怎么办?

I have read numerous helpful answers about using transactions to increment counters in Firebase, but all of these assume that the client has "read" access to the counter element. What if the client shouldn't have read access to the count, but I want to increment the value based on the user's actions?

示例:

假设我想跟踪特定内容上的观看次数或展示次数(或其他一些用户操作).计数仅供内部使用,可能与广告商和合作伙伴私下共享-但不会显示给用户本身,我们不想通过将数据公开给浏览器与竞争对手共享这些数据.

Suppose I want to track views or impressions (or some other user action) on a specific piece of content. The counts are for internal eyes only, and perhaps to be shared privately with advertisers and partners -- but not shown to the users themselves, and we don't want to share that data with competitors by exposing it to the browser.

在这种情况下,我可以通过服务器进程路由事情,在服务器进程中服务器进程获取计数并对其进行递增,但这有点违反了使用Firebase的目的.还有其他选择可从浏览器增加专用"计数器吗?

For cases like this, I could route things through a server process, in which the server process fetches the count and increments it, but that kinda defeats the purpose of using Firebase in the first place. Are there any other options to increment a "private" counter from the browser?

推荐答案

我不确定您为什么会被否决,因为这对我来说似乎是一个有效的问题.

I'm not sure why you're getting downvoted, as it seems like a valid question to me.

在Firebase中没有服务器端增加计数器的方法.有了这些知识,当前递增计数器的唯一方法是让客户端执行递增操作.尽管您可以通过确保每个用户只能递增一次的规则来确保这一点,并且每次递增都是一步之遥,并且还有很多其他事情,但客户端应用 必须能够读取当前值

There is no server-side way to increment a counter in Firebase. With that knowledge, the only way to currently increment a counter is to have the client perform the increment. While you can secure that with rules to ensure that each user can only increment once, that each increment is a single step and many other things, the client-app will have to be able to read the current value.

解决您的问题的常见方法是引入理货服务器.每个客户端都可以将它的我已经查看了此内容"记录写入数据库,然后您就有一个服务器来侦听此队列并更新计数.

The common solution to your problem would be to introduce a tally server. Each client can write it's "I've viewed this content" record into the database and then you have a server that listens to this queue and updates the count.

viewQueue
    $pushId
      uid: <uid>
      itemId: <itemId>
viewCounts
    $itemId: <totalViewCount>

您可以对此进行保护,以便用户只能写入视图队列,并且只有服务器(可能将在服务帐户下运行的服务器)可以访问视图计数:

You could secure this so that users can only write to the view queue and only the server (which likely will run under a service account can access the view counts:

{
  "rules": {
    "viewQueue": {
      ".read": false, // only the server can read this
      "$pushid": {
        ".write": "newData.exists() && !data.exists()"
      }
    },
    "viewCounts": {
      // only the server can access this
      ".read": false,
      ".write": false
    }
  }
}

这篇关于有没有办法在不向客户端公开当前计数的情况下增加Firebase中的计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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