简体   繁体   English

Node.js最佳socket.io实践(请求响应与广播)

[英]Node.js best socket.io practice (request-response vs broadcast)

I am new to node.js/socket.io and may not be asking this the right way or even asking the right question. 我是node.js / socket.io的新手,可能没有以正确的方式提出问题,甚至没有提出正确的问题。

The goal of my app is to get data from an API convert it to a JSON Object and store it in a mongodb. 我的应用程序的目标是从API获取数据,将其转换为JSON对象并将其存储在mongodb中。 Then serve the client side the data when needed. 然后在需要时为客户端提供数据。 I have thought about two possibilities and was wondering what the best practice would be. 我考虑过两种可能性,并且想知道最佳实践是什么。 My first notion was to broadcast all of the entries in the database every so often to all of the connections. 我的第一个想法是经常向所有连接广播数据库中的所有条目。 The other idea was to have the client request to server what data it needed and then send the requested data to client. 另一个想法是让客户端请求服务器提供所需的数据,然后将请求的数据发送给客户端。

The data being stored in the database is around 100 entries. 数据库中存储的数据约为100个条目。 The data would be updated from the API approximately every 30 seconds. 大约每30秒就会从API更新一次数据。 If method 1 was chosen the data would be broadcast every 5-10 seconds. 如果选择方法1,则数据将每5-10秒广播一次。 If method 2 was chosen then the data would be sent when requested. 如果选择了方法2,则将在请求时发送数据。 The client side will have different situations where not all data will be needed all the time. 客户端会遇到不同的情况,即并非始终需要所有数据。 The client side will have to request data every so often to make sure the data is "fresh". 客户端将不得不经常请求数据,以确保数据“新鲜”。

So my question is what is the best practice broadcast a large chunk every x seconds or broadcast smaller chunks when requested. 所以我的问题是,最佳实践是每x秒广播一个大块或在请求时广播一个小块。

Sorry if this doesn't make sense. 抱歉,如果这没有意义。 Thanks for your time. 谢谢你的时间。

DDP protocol is definitely an interesting way to go, but it might be overkill. DDP协议绝对是一个有趣的方法,但可能会显得过分杀伤力。 Simpler solution is to take the best from both method 1 and 2. If latency is not so important and you have spare bandwidth you can broadcast a "update" message to all clients when new data arrives. 比较简单的解决方案是从方法1和方法2中获得最大收益。如果延迟不是那么重要,并且您有可用带宽,则可以在收到新数据时向所有客户端广播“更新”消息。 The client considers if the update affects it and downloads data it needs. 客户端考虑更新是否会影响更新并下载所需的数据。

Slightly more complicated and more effective approach is subscription procedure very similar to DDP. 订阅过程与DDP非常相似,稍微复杂和有效。 For smaller projects you can implement it yourself in a while. 对于较小的项目,您可以在一段时间内自行实施。 This is how it could work: 这是如何工作的:

  • Client subscribes to a chunk of data. 客户端订阅大量数据。
  • Server sends this chunk to the client and remembers which clients subscribed to what. 服务器将此块发送给客户端,并记住哪些客户端订阅了什么。
  • If the chunk is updated the server goes through the subscription list and sends the new data to subscribers. 如果块已更新,则服务器将遍历订阅列表,并将新数据发送给订阅者。
  • Client can unsubscribe at any time by sending a special message, disconnecting (or optionally by subscribing to different chunk). 客户端可以随时通过发送特殊消息,断开连接(或选择订阅不同的块)来取消订阅。

By chunk I mean any way how to identify some data. 所谓块,是指如何识别某些数据的任何方式。 It can be record ID, time range a filter or anything that makes sense in your project. 它可以是记录ID,时间范围过滤器或在您的项目中有意义的任何内容。

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

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