简体   繁体   English

我如何在每次通过前端的鼠标单击上更新我的数据库,而不发送太多请求?

[英]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: 我想创建一个用于学习目的的小应用程序,该应用程序应该执行以下操作:

  • Sign in with Google / FB / .. 使用Google / FB / ..登录
  • React / Redux fetches users clicker count + clicker count of the top 10 clickers React / Redux获取用户的点击次数+前10个点击次数的点击次数
  • You see a highscore table with the name and click count of top 10 您会看到一个带有名称的高分表,然后点击排名前10位的计数
  • You see your clicker score and on every click you increase it 您会看到您的答题器得分,并且每次点击都会增加它
  • Now the challenging Part: 现在具有挑战性的部分:
  • I want to update the database on every click (save the click to the user in the db) 我想在每次点击时更新数据库(将点击保存到数据库中的用户)
  • Update the highscore dynamically 动态更新高分
    • So if another user clicks for example 10 times it should update his highscore 因此,如果另一个用户点击了10次,则应该更新其高分

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个问题:

  • a TON of requests which is not good, right? 一堆不好的请求,对吗?
  • if I click too fast it wouldn't be recognized right? 如果我单击得太快,它不会被识别对吗?

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 我想使用的技术堆栈

  • Node.js with Express for the Server 适用于服务器的带有Express的Node.js
  • MongoDB with Mongoose for Database (eventually mongodb cloud solution like mLab) MongoDB和Mongoose for Database(最终是像mLab这样的mongodb云解决方案)
  • React with Redux on the Frontend 在前端与Redux互动
  • React Router 反应路由器
  • Passport.js for Auth Passport.js用于身份验证

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.

相关问题 如何在我的网站的前端使用Javascript核心连接数据库? - How can i connect database with Javascript core in frontend for my website? 我如何在每 1 小时后将存储在地图中的值更新到数据库,同时同步不断更新地图的路由上的传入请求 - How can i update values stored in map to database after every 1 hour while synchronizing incoming requests on route which updates map continously 如何在不再发送所有APK的情况下更新我的应用? - How can I update my app without sending all the APK again? 发送更新和删除请求而不刷新前端 - Send a update and delete requests without refreshing frontend 在Node.js/Express中等待后端数据库更新后如何更新前端? - How can I update the frontend after waiting for the database in the backend to update in Node.js/Express? Discord.js机器人程序需要帮助-如何使我的机器人每6小时自动执行一项任务而不发送命令? - Discord.js bot help needed - how can I make my bot auto do a task every 6 hours without sending it a command? 如何防止我的功能在每次点击时运行? - How can I prevent my function from running on every click? 如何在不制作太多标签的情况下制作超链接 - How do I make hyperlinks without making too much tags 如何通过在highcharts图形上单击鼠标来更新cvs文件中的值 - How to update a value in a cvs file through a mouse click on highcharts graphs 可以将我的工具提示插件更改为接受“点击”选项而不会带来太多麻烦? - possible to alter my tooltip plugin to accept “click” option without too much trouble?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM