Node server connection event firing but request event not firing

394 Views Asked by At

I am working on a chat app where the master node is receiving request through tcp socket and after analyzing if its normal http request or web socket it is passing the socket to worker node. I see that when the socket is not read(i.e the socket.on('data') is not fired ) before it is passed to worker node ,the request event is fired but when the socket is read and then passed to worker node ,the request event is not fired on server and no routes handled.

cluster.js

// cluster.js
const cluster = require('cluster');
const net =require('net')
const os = require('os');

if (cluster.isMaster) { 
    var workers=[]
  const num_processes = os.cpus().length;
  var spawn = function(i) {
    workers[i] = cluster.fork();

    // Optional: Restart worker on exit
    workers[i].on('exit', function(worker, code, signal) {
        // logger.log('respawning worker', i);
        console.log('respawning worker', i)
        spawn(i);
    });
};

// Spawn workers.
for (var i = 0; i < num_processes; i++) {
    spawn(i);
}
//:LABEL1
var server = net.createServer(function(c) { //'connection' listener
  console.log('client connected');
  c.on('end', function() {
    console.log('client disconnected');
  });
  c.on('data',function(data){
      console.log(data.toString())
      c.pause()
      workers[1].send('sticky-session:connection', c)
  })

});
server.listen(5000, function() { //'listening' listener
  console.log('server bound');
});
//:LABEL2
//   var server = net.createServer({ pauseOnConnect: true }, function (connection) {

//     // Incoming request processing
//     var remote = connection.remoteAddress;
//     var local = connection.localAddress;
//     var cIp = remote + local;
//     var ip = cIp.match(/[0-9]+/g)[0].replace(/,/g, '');
//     // var wIndex = (ip+Math.floor(Math.random() * 10)) % num_processes;
//     var wIndex=2;
//     connection.on('end', function() {
//         console.log('client disconnected');
//       });
//       connection.on('data',function(data){console.log(data.toString())})
//     var worker = workers[wIndex];
    
//     console.log("Message to work "+ worker+", remote: "+ remote+ ", local: "+ local+", ip: "+ ip +", index: "+ wIndex)
//     // logger.log("Message to work "+ worker+", remote: "+ remote+ ", local: "+ local+", ip: "+ ip +", index: "+ wIndex);
//     worker.send('sticky-session:connection', connection);
    
// });
// server.maxConnections = Infinity;
// server.listen(5000, function() { //'listening' listener
//   console.log('server bound');
// });


} else {
  require('./app');
}

app.js

const http=require('http')
const express = require("express")

const hostname='0.0.0.0';
const port=process.env.PORT || 5001;
const app=express();
const server=http.createServer(app)
process.on('message', function(message, connection) {
    if (message !== 'sticky-session:connection') {
        return;
    }
// console.log(connection);

    // Emulate a connection event on the server by emitting the
    // event with the connection the master sent us.
    server.emit('connection', connection);
    server.on('connection',(sock)=>{console.log(sock)})//:LABEL3
    server.on('request',(req)=>{console.log(req)})//:LABEL4
    connection.resume();
});

In cluster.js , the one commented is where , the socket is not read and on connection is passed to worker node, but in uncommented one , it is read and then passed to worker node. so in uncommented code(LABEl1), the connection event of server.on('connection')(LABEL3 in app.js) is fired but event request not fired( LABEL 4 inapp.js) , but in commented code(LABEL2), both connection and event event fires (app.js) . I need to read the socket and then send it to worker node

Can anyone clarify, what is the difference and how I can solve it?

0

There are 0 best solutions below