in Education by
The idea is to create server in master process, and handle requests in workers. I want to utilize all CPU cores and to have kinda load balance as well. At first I tried to send server handler from master to worker: var cluster = require('cluster'); if (cluster.isMaster) { var app = require('express').createServer(); app.listen(1234); var worker = cluster.fork(); worker.stdin.write('fd', 'utf8', app._handle); } else { process.stdin.resume(); process.stdin.on('fd', function(fd){ var stream = require('net').Stream(fd); var io = require('socket.io').listen(stream); io.sockets.on('connection', function(socket){ ... } } } but write did not fired on('fd'...) event handler in worker. Then I put everything to master in order to check if this is possible at all: var app = require('express').createServer(); app.listen(1234); var stream = require('net').Stream(app._handler); var io = require('socket.io').listen(stream); the server starts without any errors but does not work. I cant event request the socket.io.js script from the client side with tag: Response: Cannot GET /socket.io/socket.io.js So I have two troubles: How to send opened socket descriptor to the worker? How to set up handler for the server by this descriptor? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You don't need to take care of file descriptors etc on your own, look at a similar question I've just answered here: Node.js, multi-threading and Socket.io Code sample: var cluster = require('cluster'); var http = require('http'); var numCPUs = require('os').cpus().length; if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); } } else { var sio = require('socket.io') , RedisStore = sio.RedisStore , io = sio.listen(8080, options); // Somehow pass this information to the workers io.set('store', new RedisStore); // Do the work here io.sockets.on('connection', function (socket) { socket.on('chat', function (data) { socket.broadcast.emit('chat', data); }) }); }

Related questions

0 votes
    I have working (stock) script from node var cluster = require('cluster'); var http = require(' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Step 1: server I've created a simple server with Node & Socket.io which declares a namespace under ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I get data from socket io this JSON every 10 sec.. data { "nr": "1" } data { "nr": " ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I'm trying to create Server-client connection using sockets. Server is just an Echo-server. I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I am learning about OAuth2 and OpenID Connect by experimenting with ASP.NET Core and IdentityServer4. So far ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 21, 2022 in Education by JackTerrance
0 votes
    how to proxy enable in server nodejs?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I want to send files asynchronously. I got on sending a file client->server->another client, but if ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I want to send files asynchronously. I got on sending a file client->server->another client, but if ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    I have a CKEditor in my page. Like this Facelet Title function comeBack(){ document.getElementById(editorValue ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I have a CKEditor in my page. Like this Facelet Title function comeBack(){ document.getElementById(editorValue ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I am working on my first Android Application. What I am trying to do is a POST request to a REST ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
...