简体   繁体   中英

Node.js Cluster for Multiple WebSocket Clients Connecting to Different WebSocket Servers?

I am using Node.js to implement a Websocket client that subscribes to datafeed from multiple Websocket servers.

foo = new WebSocket('ws://foo.host ...')
bar = new WebSocket('ws://barhost ...')
baz = new WebSocket('ws://baz.host ...')
qux = new WebSocket('ws://qux.host ...')

foo.on('data', data => doSomething(data))  // 5 events per second 
bar.on('data', data => doSomething(data))  // 1 events per second 
baz.on('data', data => doSomething(data))  // 1 events per second 
qux.on('data', data => doSomething(data))  // 1 events per second 

Question: If we have a multi-core system (eg. 4 cores), is it possible to make use of Node.js Cluster to load balance the processing of the incoming Websocket data, such that each core will approximately receive 2 events per second to be handled?

Or is it better to manually start 8 node.js instances and pass it an argument [foo|bar|baz|qux] for selecting the Websocket server it will connect to?

The nodejs clustering module solves one specific problem. When you have a an http server and you want to load balance incoming connections among multiple processes, that's what the nodejs clustering module does. That is not what you have. You have multiple client-side outgoing webSocket connections and you apparently want to apply multiple processes to processing the incoming data. That's completely different than what the nodejs cluster module does.

First, it's important to understand that receiving the data is not a CPU intensive process for nodejs. The actual socket processing and receiving of incoming data onto the computer is handled by the OS and is outside the nodejs process.

So, if you actually need more than one CPU to work on this, it must be to process the incoming data, not to just receive it.

There are several different ways you could structure that.

  1. You could have one central process that contains all the webSockets and then have and number of worker processes or worker threads that you pass incoming data to for processing. This would apply many CPUs to the processing of the data and would allow the load procesisng to be spread among the CPUs regardless of which socket the data arrived on.

  2. You could create 4 separate child processes and have each child process create one of the four webSocket connections and then have each child process handle just the incoming data for its webSocket. This has the disadvantage that it only applies one process to each webSocket and if most of the data comes on one webSocket, then the other processes will be largely idle.

  3. If one webSocket has a lot more load than the others and for some reason option #1 wouldn't work well, then you could combine #1 and #2. Create a separate process for each webSocket and then have some worker threads for processing the incoming data for each one. Create a work queue that incoming data is inserted into and work can be sent to each worker thread as it finishes its previous chunk of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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