0

I have following code, As there are N number of socket sending sequential stream. how to scale this using worker threads in nodejs.

const Net = require('net');
const port = 8080;
const server = new Net.Server();
const processor = require('processor.js');

server.listen(port, function() {
    console.log(`Server listening for connection requests on socket localhost:${port}`.);
});

server.on('connection', function(socket) {
    console.log('A new connection has been established.');
    
    let connectionInfo = {};
    connectionInfo.id = `${socket.remoteAddress}:${socket.remotePort}`;
    connectionInfo.partialBuffer= undefined;

    socket.on('data', function(chunk) {
        let chunkBuffer = Buffer.from(chunk);
        if(connectionInfo.partialBuffer){
            chunkBuffer = Buffer.concat([connInfo.partialBuffer, chunkBuffer]);
        }
        processor.processChunk(connectionInfo, chunkBuffer); // set connectionInfo.partialBuffer in processChunk() function 
    });

    socket.on('end', function() {
        console.log('Closing connection with the client');
    });

    socket.on('error', function(err) {
        console.log(`Error: ${err}`);
    });
});
NRaj
  • 121
  • 8
  • Probably you would move `processor.processChunk()` into a set of threads and you'd message the `chunkBuffer` to a waiting thread for it to be processed. When doing so, you will have to be aware of concurrency issues with `connectionInfo`. – jfriend00 Sep 07 '20 at 15:17
  • As i said, Stream is sequential so chunk1 should process completely and remaining chunk buffer append with next chunk2. IS there any way to listen complete socket chunks by dedicated thread. – NRaj Sep 10 '20 at 11:03
  • I don't know what "listen complete chunks by dedicated thread" means. – jfriend00 Sep 10 '20 at 13:24
  • you missed 'socket', can one socket(client) stream handle by dedicated thread. socket(client) / Thread. – NRaj Sep 11 '20 at 06:38
  • OK fine, I don't know what " listen complete socket chunks by dedicated thread" means. I do not understand your question and cannot help until your explain in more detail what you want help with. Data from a single socket arrives in a single nodejs instance and is processed by the main Javascript thread in that nodejs instance., If you want to use multiple threads, you have to create some worker threads, receive a chunk of data and then when you have a whole chunk of data, you have to pass it to a worker thread for processing. You have to write code to do that. – jfriend00 Sep 11 '20 at 16:45

0 Answers0