How to handle multiple, dynamically created listeners to a single ChildProcess in NodeJS?

56 views
Skip to first unread message

Konrad Moskal

unread,
Jun 28, 2015, 12:55:37 PM6/28/15
to nod...@googlegroups.com
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?










Konrad Moskal

unread,
Jun 29, 2015, 9:50:47 AM6/29/15
to nod...@googlegroups.com
I have already found out that this should be a reasonable solution, right? :)

//foobar.js
worker.on('message', function(responseData) {
  // assuming worker returns `{id: ..., data: ...}`
  var socket = io.sockets.sockets[responseData.id];
  socket.emit('response', responseData.data);
});

io.sockets.on('connection', function (socket) {
  socket.on('trigger', function (data) {

    worker.send({ id: socket.id, data: data });
  });
});


Sam Roberts

unread,
Jul 5, 2015, 9:04:16 PM7/5/15
to nod...@googlegroups.com
none of your code shows the definition of `io`, so its not possible to comment.
> --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+un...@googlegroups.com.
> To post to this group, send email to nod...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/5258307c-afa7-4ea9-bfbb-05c136abe487%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
http://StrongLoop.com makes it easy to develop APIs in Node, plus get
DevOps capabilities like monitoring, debugging and clustering.
Reply all
Reply to author
Forward
0 new messages