简体   繁体   English

NodeJS 轮询每个用户结构的最佳实践

[英]NodeJS Polling per User Structure best practice

My project is a full stack application where a web client subscribes to an unready object.我的项目是一个完整的堆栈应用程序,其中 web 客户端订阅了未准备好的 object。 When the subscription is triggered, the backend will run an observation loop to that unready object until it becomes ready.当订阅被触发时,后端将对那个未准备好的 object 运行一个观察循环,直到它准备好。 When that happens it sends a message to the frontend through socketIO (suggestions are welcome, I'm not quite sure if it's the best method).发生这种情况时,它会通过 socketIO 向前端发送一条消息(欢迎提出建议,我不太确定这是否是最好的方法)。 My question is how do I construct the observation loop.我的问题是如何构建观察循环。

My frontend basically subscribes to the backend, and gets a return 200 and will connect to the server per Websocket (socketIO) if it got subscribed correctly, or an error 4XX code if there was something that went wrong.我的前端基本上订阅了后端,并获得 200 的回报,如果订阅正确,它将按照 Websocket (socketIO) 连接到服务器,或者如果出现问题,则会出现错误 4XX 代码。 On the backend, when the user subscribes, it should start for that user, a "thread" (I know Nodejs doesn't support threads, it's just for the mental image) that polls an information from an api every 10 or so seconds.在后端,当用户订阅时,它应该为该用户启动,一个“线程”(我知道 Nodejs 不支持线程,它只是为了心理形象)每隔 10 秒左右从 api 轮询一次信息。

I do that, because the API that I poll from does not support WebHooks, so I need to observe the API response until it's at the state that I want it (this part I already got cleared).我这样做是因为我从中轮询的 API 不支持 WebHooks,所以我需要观察 API 响应,直到它位于我想要的 state 处(这部分我已经清除了)。

What I'm asking, is there a third party library that actually is meant for those kinds of tasks?我要问的是,是否有第三方库实际上适用于这些任务? Should I use worker threads or simple setTimeouts abstracted by Classes?我应该使用由类抽象的工作线程还是简单的 setTimeouts? The response will be sent over SocketIO, that part I already got working as well, it's just the method I'm using im not quite sure how to build.响应将通过 SocketIO 发送,这部分我已经开始工作了,这只是我正在使用的方法,我不太确定如何构建。

I'm also open to use another fitting programming language that makes solving this case easier.我也愿意使用另一种合适的编程语言来更容易解决这个问题。 I'm not in a hurry.我不着急。

A polling network request (which it sounds like this is) is non-blocking and asynchronous so it doesn't really take much of your nodejs CPU unless you're doing some heavy-weight computation of the result.轮询网络请求(听起来像这样)是非阻塞和异步的,因此除非您对结果进行一些重量级的计算,否则它实际上不会占用您的 nodejs CPU 太多。

So, a single nodejs thread can make a lot of network requests (for your polling and for sending data over socket.io connection) without adding WorkerThreads or clustering.因此,单个 nodejs 线程可以发出大量网络请求(用于轮询和通过 socket.io 连接发送数据)而无需添加 WorkerThreads 或集群。 This is something that nodejs is very, very good at.这是 nodejs 非常非常擅长的事情。

I'm not aware of any third party library specifically for this as you have to custom code looking at the results of the network request anyway and that's most of the coding.我不知道有任何专门用于此的第三方库,因为无论如何您都必须自定义代码来查看网络请求的结果,而这就是大部分编码。 There are a bunch of libraries for making http requests of other servers from nodejs listed here .有一堆库用于从此处列出的 nodejs 向其他服务器发出 http 请求。 My favorite in that list is got() , but you can look at the choices and decide what you like.该列表中我最喜欢的是got() ,但您可以查看选项并决定您喜欢什么。

As for making the repeated requests, I would probably just use either repeated setTimeout() calls or a setInterval() call.至于发出重复请求,我可能只会使用重复的setTimeout()调用或setInterval()调用。

You don't say whether you have to make separate requests for every single client that is subscribed to something or whether you can somehow combine all clients watching the same resource so that you use the same polling interval for all of them.您没有说是否必须为订阅某项内容的每个客户端发出单独的请求,或者您是否可以以某种方式组合所有正在观看相同资源的客户端,以便您对所有客户端使用相同的轮询间隔。 If you can do the latter, that would certainly be more efficient.如果你能做到后者,那肯定会更有效率。

If, as you scale, you run into scaling issues, you can then move the polling code to one or more child processes or WorkerThreads and then just communicate back to the main thread via messaging when you have found a new state that needs to be sent to the client.如果在扩展时遇到扩展问题,则可以将轮询代码移动到一个或多个子进程或 WorkerThreads,然后在找到需要发送的新 state 时通过消息传递回主线程给客户。 But, I would not anticipate you would need to code that extra step until you reach larger scale.但是,我预计在您达到更大的规模之前,您不需要编写额外的步骤。 As with most scaling things, you would need to code up the more basic option (which should scale well by itself) and then measure and benchmark and see where any bottlenecks are and modify the architecture based on data, not speculation.与大多数可扩展的东西一样,您需要编写更基本的选项(它本身应该可以很好地扩展),然后测量和基准测试并查看瓶颈在哪里,并根据数据而不是推测来修改架构。 Far too often, the architecture is over-designed and over-implemented based on where people think the bottlenecks might be rather than where they actually turn out to be.很多时候,架构被过度设计和过度实施是基于人们认为瓶颈可能在哪里,而不是实际出现在哪里。 Not only does this make the development take longer and end up with more complicated implementation than required, but it can target development at the wrong part of the problem.这不仅会使开发花费更长的时间并最终实现比所需的更复杂的实现,而且它可能会针对问题的错误部分进行开发。 Profile, measure, then decide.分析,测量,然后决定。

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

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