Home > other >  Access socket ref to pass to child process
Access socket ref to pass to child process

Time:11-18

Using Node.js, I can pass a handle/ref for a http request to a child process using this construct:

const k = cp.fork(childPath);

const server = http.createServer((req,res) => {
  k.send('foo', req.socket);
});

and in the child process, I can do:

process.on('message', (m, socket) => {
    socket.write([
      'HTTP/1.1 200 OK',
      'Content-Type: text/html; charset=UTF-8',
      'Content-Encoding: UTF-8',
      'Accept-Ranges: bytes',
      'Connection: keep-alive',
    ].join('\n')   '\n\n');
  
    socket.write(`
    <h1> Example </h1>
  `);
    
    socket.end('foobar');
};

which is pretty cool, allows child processes to write directly to requests.

I am wondering, how I can do the same for a websocket server:

const ws = require('ws');
const server = new ws.WebSocketServer({port: 5151});

I assume passing a connection to a child process should work:

server.on('connection', c => {
   k.send('foo', c);  /// error here
});

but I get an error, saying:

  • Related