Hello!
So, I think I made a design mistake.
I have a child process worker, that receives some data and sends back the results to dynamically attached listener.
Simplified code:
//app.js
var worker = childProcess.fork('./app_modules/workers/worker1.js');
worker.setMaxListeners(0);
require('./app_modules/sockets-user/foobar.js')(io, worker);
//foobar.js
io.sockets.on('connection', function (socket) {
socket.on('trigger', function (data) {
worker.send(data);
worker.once('message', function(responseData) {
//here I get a response from worker
socket.emit('response', responseData);
});
});
});
It was working great until I discovered that If socket.on('trigger' is triggered at the very exact moment by different users every listener would receive the same message.
I could change worker.once to worker.on and filter incoming messages but its not really a fix. I would have to destroy every listener after it was used to prevent them from adding up and I don't know if it is even possible.
I did pass the worker object like that, because I thought I could pass it anywhere it was needed, now I feel like I was wrong. How should I approach this problem?