How could I reuse the redis connection in processes created using Childprocess.fork() in nodeJs ?
For example, I would have a redis-config.js file where I create an instance of RedisClient and connect to the redis server. So after that I would have another file, test.js for example, and inside that file test, js I would access the instance of redis via import and then I could for example insert a key in redis. But this without creating another connection to the redis server.
For example
redis-conf.js
const Redis = require('redis');
const redisClient = Redis.createClient({name:'test-conecction'});
module.exports = {redisClient:Object.freeze(redisClient)}
main.js
const cp = require('node:child_process');
const {redisClient} = require('./redis');
redisClient.connect().then(()=>{ <<=== this is want i want, this does not work, i want to connect only once.
const c1 = cp.fork('./child.js');
const c2 = cp.fork('./child.js');
})
child.js
const {redisClient} = require('./redis');
redisClient.info().then((data)=>{ <<== when i try this, nodeJs tells that the client is disconnected
console.log(data);
})
IN resume. Its not possible. Because when nodeJs create a childPorcess evething is re-created and all resources are re-alocated.