简体   繁体   English

如何有效地在 MEAN 网络应用程序上实现类似功能?

[英]How to implement a like feature on a MEAN web app efficiently?

I am building a web app wherein a user can like some choices displayed on the page.我正在构建一个网络应用程序,其中用户可以喜欢页面上显示的一些选项。

I want to build this like/unlike system in the most efficient way possible.我想以最有效的方式构建这个喜欢/不喜欢的系统。 Does every press of the like button need to send an http request to the node.js server to modify user data in Mongo?每次按like按钮都需要向node.js服务器发送http请求来修改Mongo中的用户数据吗?

I'm asking since I will be having a python script as a recommender system that listens to every change happening in MongoDB.我问是因为我将有一个 python 脚本作为推荐系统,它会监听 MongoDB 中发生的每一个变化。

Yes, every click should go to the server by making a callback. 是的,每次单击都应通过进行回调来到达服务器。 Someone can say that: 有人可以说:

you can also do tweaks with this functionality like pop all the ids of posts liked by a specific user in an array and send it back at the end of its session or after a specific amount of time. 您还可以使用此功能进行调整,例如在数组中弹出特定用户喜欢的所有帖子的ID,然后在会话结束时或特定时间后将其发送回去。

But think what if that array somehow lose the data by mistake ? 但是,如果该数组由于某种原因错误地丢失了数据怎么办? Or the session is failed due to some reasons? 还是由于某些原因会话失败? Also, how will other users see that which post is liked or not ? 另外,其他用户将如何查看喜欢或不喜欢的帖子?

See these are the reasons we always send the response back each time. 看到这些是我们总是每次回送响应的原因。 However JQuery and other frameworks are there to make it fast. 但是,JQuery和其他框架可以使其快速运行。

Does every press of the like button need to send an http request to the node.js server to modify user data in Mongo? 每次按like按钮是否需要将http请求发送到node.js服务器以修改Mongo中的用户数据?

You need to get your data to the server somehow, yes. 是的,您需要以某种方式将数据发送到服务器。 An HTTP request is generally a good choice, and doesn't have to be as heavyweight as it once was. 通常,HTTP请求是一个不错的选择,并且不必像以前那样繁重。

Firstly, your server should be enabling HTTP keep-alive, where the underlying TCP connection stays open for some amount of time once the request is finished. 首先,您的服务器应启用HTTP保持活动状态,请求完成后,基础TCP连接将保持打开状态一段时间。 That way, subsequent requests can be made on the same connection. 这样,可以在同一连接上进行后续请求。

Additionally, you should ensure you have HTTP/2 enabled, which is a more efficient protocol due to its binary nature. 此外,您应确保已启用HTTP / 2,由于其二进制性质,这是一种更有效的协议。 More importantly, headers like Cookie and what not aren't sent over and over again. 更重要的是,不会重复发送Cookie标头和不发送的标头。

By following these best practices, you'll find that your request/responses are just a few bytes down the wire of an existing connection. 通过遵循这些最佳实践,您会发现您的请求/响应仅是现有连接的几字节。 And, you won't have to change anything in your code to do it! 而且,您无需更改代码中的任何内容即可!

If you're expecting thousands of users, then using a traditional RESTful api might not be the best option here. 如果您期望成千上万的用户,那么使用传统的RESTful api可能不是此处的最佳选择。 I'd recommend looking into websockets for your use case. 我建议您针对用例研究websocket。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM