In my master node, a tcp socket is intercepting all requests, where I want to separate normal http requests and websocket ones. I need the wobsocket connections to be handled by a particular node in a time (say worker 5 handles all connections until socket connections reaches 10 and the next 10 will be handled by worker 6 say) . For that its checking http packet within it and sending the socket to apt node instance via IPC , but I am unable to fire up request object for express routes. The server in worker node is receiving connection event as indicated in app.js file as :LABEL but none of my express route is working

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);
}

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');
});
//   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');
// });
// net.createServer({pauseOnConnect:true},(connection)=>{
//     console.log(connection)
// }).listen(5000)

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

server.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)})//:LABEL
    connection.resume();
});

Also I need suggestion to go this way, of sending request to specific worker node programmatically and not leaving it to the node cluster to do it. The other way I know to handle socket io and node cluster together is through redis adapter but due to some ease of work I am considering this way

0

There are 0 best solutions below