C
C
cat_crash2012-10-11 18:35:42
Node.js
cat_crash, 2012-10-11 18:35:42

How to fork on server.on('connection'?

Good day, node lovers.
Please do not kick too much for the question - I'm still new to the node.
Actually task: to create each time child process (worker) at new connection to the server.

var dnode  = require('dnode');
var net = require('net');
var cluster = require('cluster');

if (cluster.isMaster) {

  cluster.fork();
  
} else {

  var server = net.createServer(function (c) {
      var d = dnode({

                      //some code here			
      })
      c.pipe(d).pipe(c);
  });
    
  server.listen(5050);
  
  server.on('connection', function(id) {
    cluster.fork();
  });
  
  
}

throws a non-obvious error
assert.js:102
throw new assert.AssertionError({
^
AssertionError: false == true
at Cluster.cluster.fork (cluster.js:469:3)
at Server.(/home/nodejs/test.js: 52:11)
at Server.EventEmitter.emit (events.js:123:20)
at TCP.onconnection (net.js:1038:8)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wickedweasel, 2012-10-23
@wickedweasel

// Fork a new worker
cluster.fork = function(env) {
  // This can only be called from the master.
  assert(cluster.isMaster);
  
  // Make sure that the master has been initialized
  cluster.setupMaster();
    
  return (new cluster.Worker(env));
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question