-1

I'm new to Node.js, and Socket.io, and for my current web development project, I have a Socket.io chatroom set up, and I need to be able to make lots and lots (Potentially hundreds) of seperate instances of it. Is there any way to do this?

Will6316
  • 71
  • 7
  • Better you should explain what's your use case, so we can advice you more. In essence you should have one instance per server, so if you want hundred of instances, then create hundred of servers. But it doesn't seems very natural to have hundred of socket.io instances. – Hosar Feb 05 '17 at 01:52
  • Why don't you just use one server and have lots of clients use it? There should be no scale issue with chatroom server. You should be able to host thousands of users with one server. That is the normal use case for a server. – jfriend00 Feb 05 '17 at 05:09

1 Answers1

0

Spawning a child process in node is pretty simple, especially if it's another node process.

child_process.fork is one of the available options

import cp = require('child_process');

var proc = cp.fork('path/to/module');
proc.on('error', function(err) {
  console.error('The process failed to start');
  console.error(err);
});

proc.on('exit', function(code, signal) {
  console.log('The process was terminated by ' + signal + ' and exited with code ' + code);
});

You may also want to look at cluster, it uses child_process.fork to spawn the children, but has additional functionality built on top to automatically share resources between processes, like a server handle.

Although I don't know exactly how you plan to use the hundreds of children, I would recommend limiting the children to the number of cores available on the machine. Spawning hundreds of children could hinder performance. You should rely on node's asynchronous nature to handle scalability, spawning workers doesn't really help much beyond balancing the processing load across the available CPUs.

Jake Holzinger
  • 5,783
  • 2
  • 19
  • 33
  • While this is technically an answer to creating lots of instances, this is probably not what the OP should do. They should probably have lots of users using one server, not lots of servers. – jfriend00 Feb 05 '17 at 05:10
  • Oh, and you also can't have all those server instances listening to the same port either. – jfriend00 Feb 05 '17 at 05:19