Reputation: 165
I am developing an app with nodeJS and cluster with multiple workers with different port on each worker, I may need to make the workers communicate each other, I know that nodeJS cluster has built in support for messaging between master and other workers.
I have 3 questions regarding this. 1 . can I send the message between workers without master to be in the middle, for faster process ?
Is it good idea to open a websocket on each worker to listen from other workers to replace the built in messaging in cluster, is it faster ?
if the app would scale to multiple servers I think websocket would be the answer, any alternatives please suggest ?
Upvotes: 0
Views: 260
Reputation: 2239
IPC.
Each NodeJS child process (started with fork) has process.send(<data>);
method. And can listen for messages from parent. Feel free to pass objects, if you want. It will be stringified and parsed on other side.
process.on('message', (data) => {});
When you fork process on parent side - it also has methods for messaging.
const child = child_process.fork('child.js');
child.on('message', (data) => { });
If you want to make workers/childs communicate between each other - You can manage it by sending specific messages to master, and master will forward it to specified worker.
Docs: Child Process Event "Message"
Upvotes: 0
Reputation: 56557
Upvotes: 1