Home > Software engineering >  Dedicated Express and Socket.IO servers
Dedicated Express and Socket.IO servers

Time:03-23

I have a nodejs web app using Expressjs and a SocketIO server on the same port. The web app has register, login and dashboard pages. with Websocket, guest or registered users can chat together.

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");

const io = new Server(server);

app.get('/', (req, res) => {
  //
});

app.get('/auth', (req, res) => {
  //
});

app.get('/dashboard', (req, res) => {
  //
});

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

server.listen(3000, () => {
  console.log('listening on *:3000');
});

Should i separate web and socketio as standalone projects? i'm worried because the websocket server / socketio will later be scaled out. The web app may also need to be scaled.

What is the best architecture for such a project?

I'm a beginner and not familiar with scaling nodejs apps so sorry for my question.

Take Crisp as an example.

CodePudding user response:

Run the HTTP and Socket.IO server in the same process, later when you want to scale horizontally introduce load balancing to distribute requests to multiple instances of your server.

With multiple instances you will also have multiple Socket.IO servers that will need to pass messages between each other so events can reach clients indifferent to which instance they are connected to, this is handled by an Adapter that usually uses Redis for communication, take a look here for more info about this: https://socket.io/docs/v4/using-multiple-nodes/

  • Related