KMK
KMK

Reputation: 1519

How to run multiple node.js servers

I have two Express servers running on different ports. The first server (web server) provides a website and the second server (data server) emits data every 100 ms using socket.io. When I open the website in the browser the data appears as expected. But when I then open the website in a second browser, all the emitted data in the first browser slows remarkably down until the second browser is done loading the site.

I should probably mention that the application is running on a Raspberry Pi. The website loading time is not so critical, but the data emission is.

Is there some way to run the node.js servers so they won't be affected by the load on each other?

Right now the web server is in the main app.js file that is run by node and the data server is a module required by the web server. Something like this:

app.js:

var express = require('express');
var data = require('data-server.js');

var app = express();

// Web server setup and handling (mostly Express defaults)

app.listen(3000);

module.exports = app;

data-server.js:

var app = require('express')();
var server = require('http').Server(app).listen(3001)
var io = require('socket.io')(server);

// Socket.io code that emits data every 100 ms

module.exports = io;

Update

Turns out it was my database query that was holding on to node so it couldn't emit because the emission function was waiting for the database to be free.

Upvotes: 4

Views: 14973

Answers (2)

aakash4dev
aakash4dev

Reputation: 1176

multiple-node-serversMethod 1: Infinite-Servers.js : Start 1000 servers in one run

const express = require('express')
var app=new Array(10)

for (let i = 3000; i <= 4000; i++) {
    app[i]=express()
    app[i].get('/',(req,res)=>res.send({'port': i}))
    app[i].listen( i,()=>console.log('port', i,"started"))    
}

Method 2: MultiServers.js : Start 2 servers:

const express = require('express')
server=express()
server2=express()

server.get('/',(req,res)=>res.send({"server":"3001","name":"aakash4dev","data":"some data 1"}))

server2.get('/',(req,res)=>res.send({"server":"3002","name":"aakash4dev","data":"some data 2"}))
server2.get('/somethig',(req,res)=>res.send({"server":"3002","name":"aakash4dev","data":"some data 2"}))

server.listen(3001)
server2.listen(3002)

Method 3: SepetareInfiniteServers.js

If you want to keep all files separately, e.g. port1.js port2.js port3.js .... port1000.js, all in different files and start all servers on a single run, then use module.exports (official docs: https://nodejs.org/api/modules.html#moduleexports ) along with the first InfiniteServers.js codes above. codes will look like this:

var portsData=new Array(3)

 const express = require('express')
 const cors=require('cors')
 var app=new Array(3)
 
 for (let i = 1; i <= 3; i++) {
     portsData[i]=require('./port'+i)
     app[i]=express()
     app[i].use(cors())
 
     app[i].get('/',(req,res)=>{
         let req_requestedData={"name":"aakash4dev"}
         res.send(require('./port1.js').fun1({req_requestedData,"our-Response":"Im server"}))
     })
     
     app[i].listen( i,()=>console.log('port', i,"started"))    
 }

note: import cors module and use app[i].use(cors()) in array or app.use(cors()) in normal servers to remove error cors policy no 'access-control-allow-origin'

Upvotes: 0

Tristan Foureur
Tristan Foureur

Reputation: 1657

You should use the same port and the same node process for both your regular HTTP traffic and your websocket traffic. It would minimise the performance impact on your raspberry pi. Example :

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

Upvotes: 4

Related Questions