I have this code in an index.js that I mostly left unmodified from the official Node.js documentation.
const https = require('https');
const fs = require('fs');
const cluster = require('node:cluster');
const numCPUs = require('node:os').cpus().length;
const process = require('node:process');
const livegame = require('./server-livegame');
const matchmaking = require('./server-matchmaking');
//Start ExpressJS
var express = require('express');
const { match } = require('assert');
var app = express();
app.use(express.json());
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
for (let i = 0; i< numCPUs; i++){
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died`);
});
}
else
{
new livegame;
new matchmaking;
}
Here is a simplified code for livegame/matchmaking that produces the error.
const https = require('https');
const fs = require('fs');
const mongoose = require('mongoose');
//Import Models
const LiveMatch = require('./models/livematch');
//Start ExpressJS
var express = require('express');
const { match } = require('assert');
//Interface Security
var options = {
key: fs.readFileSync('key.pem', 'utf8'),
cert: fs.readFileSync('cert.pem', 'utf8')
};
//Server
var httpsServer = https.createServer(options, app);
httpsServer.listen(443);
var app = express();
app.use(express.json());
const chat =
{
puuid: String,
name: String,
roleID: String,
message: String,
}
app.post(':id/chat', (req,res) =>
{
//something here
});
I have livegame and matchmaking as separate .js files alongside the index.js whom I call to launch them programmatically as multiple instances. However, this is the error I get:
node:events:505
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::443
at Server.setupListenHandle [as _listen2] (node:net:1380:16)
at listenInCluster (node:net:1428:12)
at Server.listen (node:net:1516:7)
at C:\Users\----\Documents\MG\src\server-matchmaking.js:25:66
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1407:8)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 443
}
Based on my progress so far, I think the issue is with me creating more "dining space" instead of hiring more "workers".
How do I properly create instances of a server with proper load balancing using clusters?
Clustering in nodejs, by its very definition is a group of processes that are all set up to process incoming connections the same. The master process uses an algorithm to rotate incoming connections among the various clustered processes so that each one gets a turn handling incoming connections. This is typically used when you need to increase the number of CPUs that you use for processing incoming http requests. By default a single nodejs process runs your Javascript in only a single thread/CPU.
And, by definition, these clustered processes are all doing the same thing (handling incoming http requests) as you are doing in the main process.
If, what you really want is to run different code (lets say you were going to do some heavy duty image processing) and you rightly want to get that heavy CPU processing out of main thread so it can remain responsive to incoming connections, then you would use either a WorkerThread (more threads in the same process) or a child_process (more processes). In both of those cases, you can run whatever code you want in the WorkerThread or child_process and it can be completely different than what you're doing in your main nodejs program.
If the underlying issue is some processing you're doing in a POST handler on your web server, then whether or not you need to investigate any of the above ways to scale depends entirely upon what you're doing in that POST handler. Nodejs by itself, with its asynchronous I/O model (for networking, file I/O, database access, etc...) can handle a ton of simultaneous requests without involving additional threads or processes. Properly written asynchronous code scales really well in nodejs.
So, unless your process is heavily using the CPU (as in the image processsing example I used above), then you should just write good asynchronous code first, see how that works and perhaps load test it to find where your bottlenecks are. You may indeed find that your bottlenecks are insigificant (thus you can get scale quite high) or they are not in a place that would benefit from the above additional processes and you need to attack the specific causes of the bottlenecks. With any high scale design, you first implement the basics, then instrument at scale to understand your bottlenecks, measure again, work on the bottlenecks, measure again, etc...
Guessing where your bottlenecks are is usually very error prone which causes you to put software engineering into the wrong places. Don't guess. Test, measure, find bottlenecks, work on bottlenecks. Rinse, lather, repeat.
Building on #krtee 's post:
The core problem here is that you're essentially trying to create multiple processes on the same port.
This is true, and you're really close to the node docs on this!
// essentially all this stuff
var httpsServer = https.createServer(options, app);
httpsServer.listen(443);
var app = express();
...
// needs to go in the `else` block of the worker initialization
...
for (let i = 0; i< numCPUs; i++){
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died`);
});
}
else
{
// right bout here.
}
While I might not go with clustering, here is how I fixed this piece of code.
const https = require('https');
const fs = require('fs');
const cluster = require('node:cluster');
const numCPUs = require('node:os').cpus().length;
const process = require('node:process');
const livegame = require('./server-livegame');
const matchmaking = require('./server-matchmaking');
//Start ExpressJS
var express = require('express');
const { match } = require('assert');
var app = express();
app.use(express.json());
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
for (let i = 0; i< numCPUs; i++){
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died`);
});
}
else
{
var httpsServer = https.createServer(options, app);
httpsServer.listen(443);
livegame(app);
matchmaking(app);
}
with livegame/matchmaking.js looking like
const https = require('https');
const fs = require('fs');
const mongoose = require('mongoose');
//Import Models
const LiveMatch = require('./models/livematch');
//Start ExpressJS
var express = require('express');
const { match } = require('assert');
this.serverLiveGame = function(app)
{
const chat =
{
puuid: String,
name: String,
roleID: String,
message: String,
}
app.post(':id/chat', (req,res) =>
{
});
};
module.exports = this.serverLiveGame;
There was more than one mistake in the code:
There can be only be one express() function handling the API for a given server. This value should be constant for any related APIs running under the same port. In my implementation, I simply pass the variable to the functions as a parameter.
To properly pass the api to the worker method from another .js, they must be passed on as a module with a proper module.exports. I wrapped the necessary code in a function so I only need to call one thing per api.
Since we are calling a function and not creating a new object, "new" is incorrect. We call it as any other function with function(param);
Thank you for all the help!
I have built a backend and wanted to test it with some front-end code but for whatever reason, it is just not firing the events on the server. I dont get the "on connect" event triggered. I already downgrade the backend to a version similar to my testing script but still all the server shows, are logs like this.
[22/Oct/2021:14:06:21 +0000] "GET /socket.io/?EIO=4&transport=polling&t=NoeKlFT&b64=1 HTTP/1.1" 404 149 "-" "node-XMLHttpRequest"
I am using socket io for server and client version 3.1.2
Here is the backend code snippet
const dotenv = require("dotenv").config();
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io")(server);
const morgan = require("morgan");
const cors = require("cors");
const config = require("./src/config/general");
const exchangeController = new ExchangeController(io); // I want to pass in the IO obejct to be able to deal with all the event stuff in another file besides the apps entry point file.
Here is the part where I want the IO object to be available and work with the events from there
class ExchangeController {
constructor(io) {
this.io = io;
this.exchangeService = new ExchangeService();
this.router = express.Router();
this.initRoutes();
this.io.on("connection", (socket) => {
console.log("incoming socket connection"); //gets never logged to console when connecting frontend
//do further stuff (deleted for post)
});
});
}
This is the frontend script for testing the connection
//client.js
const io = require('socket.io-client');
const socket = io("mysecreturl") //also tried localhost instead of deployed app url no difference
// Add a connect listener
socket.on('connect', function (socket) {
console.log('Connected!');
});
I really tried for way too long now without getting any results. I hope you guys can guide me on the right path again.
Cheers
Why am I getting a segmentation fault error whenever I type in any node commands?
A bit of background information: I'm trying to deploy any basic demo node app (in GoDaddy's shared hosting) following these instructions (from the comment from user called 'x.g.'). I do everything and get to the very last instruction (number 5) where it states to type node app.js & and I get the following response in the terminal:
[1] 326516
If I type node app.js without the ampersand & I get this error:
Segmentation fault
Does anyone know why this happens and how to fix it? I basically have (as per the instructions) an app.js with this simple code:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('NodeJS server running on Shared Hosting\n');
});
server.listen(port, hostname, () => {
console.log('Server running at http://${hostname}:${port}/');
});
Also, whenever I type anything like node, npm or node-v it also states the Segmentation fault error. Any help would be much appreciated!
Update: any ideas anyone?
I am getting this error:
WebSocket connection to 'ws://localhost:8000/socket.io/?EIO=3&transport=websocket' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
I have looked for this error but it seems for me that my configuration of the sockets is well and I do not think is for the warning of electron.
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var JSONTCPSOCKET = require('json-tcp-socket');
var JSONTCPSOCKET = new JSONTCPSOCKET({tls: false});
require("./rabbit")(io, JSONTCPSOCKET);
var socket = io('http://localhost:8000',{transports: ['websocket',
'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling', 'polling']});
Any idea?
Thanks mates!!
It was a silly mistake.
The file that contains the server that listen in that port is in my case server.js.
When you run it with node, the start file is server.js but when you run it with electron the start file is main.js and I was never running server.js when I executed with electron, so I was not listening in that port.
I have an express.js application and it has to run a sub-process everytime there is a particular request (here it is : /compute/real-time ). There will be user-created scripts to compute the data. So, I am using node cluster module to create a pool of workers and pick the one which is free to execute the scripts. But I have hit the wall during the creation of cluster itself. Here is the code
clusterPool.js
var cluster = require('cluster');
exports.setupCluster = function(){
console.log ("Setting up cluster for bot processing " )
if (cluster.isMaster){
cluster.fork(); //one is good enough at the moment
}
}
compute.js
var async = require('async');
var clusterPool = require('.././clusterPool')
//Start the cluster
clusterPool.setupCluster();
exports.computeRealTime = function(req,res){
clusterPool.cluster.on("listening",function(worker){
//....Do something with the worker
})
}
webserver.js
// Include Express
var express = require('express');
var compute = require('.././compute');
// Create a new Express application
var app = express();
// Add a basic route – index page
app.get('/compute/real-time',compute.computeRealTime);
// Bind to a port
app.listen(3000);
Here is the error I am facing :
error: code=EADDRINUSE, errno=EADDRINUSE, syscall=bind
error: Error: bind EADDRINUSE
at errnoException (net.js:884:11)
at net.js:1056:26
at Object.1:2 (cluster.js:587:5)
at handleResponse (cluster.js:171:41)
at respond (cluster.js:192:5)
at handleMessage (cluster.js:202:5)
at process.EventEmitter.emit (events.js:117:20)
at handleMessage (child_process.js:318:10)
at child_process.js:392:7
at process.handleConversion.net.Native.got (child_process.js:91:7)
IS there any way out for this problem please?
Thanks in advance
This is one of the cases, when your server code faces an error and due to improper error handling, we get exception but the port is still busy. So you need to clear out the application which has reserved that port. If you are using linux, you can use
lsof -i :3000
Get the process id and kill that process using
kill -9 #processId
Then restart your server.