The Question
Why isn't it possible to send more than messages (strings) to forked subprocesses in NodeJS? This answer simply states that is isn't possible but provides no explanation.
How this question came about
I need to send database connection to subprocesses to avoid connecting to those databases multiple times.
My program connects to four different services: Redis, OrientDB, Postgres, and ElasticSearch.
I'm doing some heavy lifting, and need to use multiple processes and have all of the processes connect to these services. I've made an object to hold all of these clients:
var clients = {
redisClient: /* redis connection */
orientClient: /* orientdb connection */
pgClient: /* postgres connection */
elasticClient: /* elasticsearch connection */
};
But when I send the clients to subprocesses:
var child = cp.fork(__dirname + '/singleCrawler.js');
child.send(clients);
I get the following error:
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at ChildProcess.target.send (child_process.js:451:23)
I'm not the only one having this problem.
My solution to the problem
I'm going to spawn another process that handles the services. The other processes will communicate with this process to interact with the services.