Node.js pass handle of response object handle to child process
Asked Answered
H

1

9

I have an http server and a forked child process. I want the parent to receive requests and pass to forked process using worker.send. and the worker should be able to process and send the response back to the requester using the same response object.

I tried sending the response object in the second parameter of worker.send, but it gives the error This handle type can't be sent

var child_process = require('child_process');

var worker = child_process.fork(filename);

http.createServer(function (req, res) {
    worker.send({ 'event': 'start' }, res); // send response object
}).listen(4000);

I checked in the child_process.js file and it says if it doesn't belongs to some of the types, it will throw the error.

I want to know if there are any other options by which I can send the response object to the forked child.

EDIT:

Okay, here's what I found, I just changed the following

// Instead of
// worker.send({ 'event': 'start' }, res);
worker.send({ 'event': 'start' }, res.socket);

And the forked process is able to call write on the handler it gets.

Is it correct? Can I use it this way? or will there be any implications under some blah blah conditions?

Hegelianism answered 16/7, 2013 at 11:17 Comment(2)
Hi @Салман. Did you get any solution for this problem? I'm stuck with the same issue.Nanceenancey
Nope, but I think I managed to solve it some other way not with this approach. If you have specific use case that you can share, you could find a solution.Hegelianism
C
0

The worker.send only takes requests coming from a net.createServer function. Try better with 'net' package instead of http. This resolved my issue

Charleton answered 25/7, 2016 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.