[英]How can I update my database on every mouse click through the frontend without sending too much requests?
Following Problem: 以下问题:
I want to create a little app for learning purposes that should do following things: 我想创建一个用于学习目的的小应用程序,该应用程序应该执行以下操作:
I have a little chart to visualize this better (notice it has a 2nd tab on the bottom where I describe the stack I want to use) 我有一个小图表可以更好地形象化(请注意,它的底部有一个第二个选项卡,用于描述我要使用的堆栈)
My thoughts 我的想法
I think I have to figure something out to not constantly update the database. 我认为我必须弄清楚一些事情,以便不不断更新数据库。 Because if I update it on every click I see 2 problems:
因为如果我每次点击都更新它,就会看到2个问题:
What I wonder is, I think I created something similiar but much more simplified a few months ago to test out firebase from google. 我想知道的是,我想我在几个月前创建了类似的东西,但进行了简化,以测试来自Google的firebase。 I saved every click to firebase.
我将每次点击都保存到了Firebase。 I also had a listener with
on.("value")
or something like that, that created an action every time a new "click" comes in. 我还有一个监听器,它带有
on.("value")
东西,每次新的“ click”进来时都会创建一个动作。
This worked perfectly, even when I clicked very fast. 即使我单击得非常快,这也能很好地工作。 How is this possible?
这怎么可能? How is firebase doing this?
Firebase如何做到这一点?
Tech Stack I want to use 我想使用的技术堆栈
Now I would like to know if my approach I have in plan is going to work or if this would even work with that kind of stack. 现在,我想知道我计划中的方法是否可以使用,或者甚至可以在这种堆栈中使用。 I didn't find anything on google that could help me out with this kind of question(s).
我在Google上找不到任何可以帮助我解决此类问题的信息。 So how can I update the db to reflect the data I see in my app but not creating an api call per click?
那么,如何更新数据库以反映我在应用程序中看到的数据,而不是每次点击都不会创建api调用?
I recommend you looking into using websockets . 我建议您考虑使用websockets 。 This will probably be what you're looking for to help you update the database close to real-time.
这可能是您要寻找的帮助您接近实时更新数据库的内容。 But make sure that you're keeping track of the current user's click in the React/Redux state.
但是请确保在React / Redux状态下跟踪当前用户的点击。
Another solution is to throttle your REST api calls to update the database. 另一个解决方案是限制您的REST api调用以更新数据库。 The downside of this is the other users in your application won't see the real-time value of every other users.
缺点是应用程序中的其他用户不会看到其他所有用户的实时价值。 But since you're throttling it'll only be slightly behind.
但是由于您节流,它只会稍微落后一点。
https://css-tricks.com/the-difference-between-throttling-and-debouncing/ https://css-tricks.com/the-difference-between-throttling-and-debouncing/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.