Home > Net >  How use multiple socket.io servers with different library version on the same Node.js application
How use multiple socket.io servers with different library version on the same Node.js application

Time:01-30

As the title suggests I'm trying to use in the same application two different socket.io servers, one with socket.ioV4 and the other with socket.ioV2, the servers go up but I fail to get the sockets to connect.

A bit of context: the reason I'm trying to do this is because two clients need to exchange data using the Node.js application as a bridge, and each of them requires a different version of the socket.io package (as mentioned, V4 vs V2).

What I managed to do: I installed the V4 version as usual and the V2 version using an alias

npm install socket.io@^4.4.1

npm install socket.io-v2@npm:[email protected]

and I created the two servers with

const express = require('express');
const http = require('http');
const { Server } = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = new Server(server);

const v2_server = require('http').Server(app);
const iov2 = require('socket.io-v2')(v2_server);

The socket.io servers are defined as such

io.on('connection', (socket) => {
    // DEBUG
    console.log("V4 data received");
})

iov2.on('connection', (socket) => {
    // DEBUG
    console.log("V2 data received");
})

Then I spin up the two servers on two different ports

app.get("", (req, res)=>{
res.sendFile(__dirname   '/static/test.html')
})


server.listen(conn_port, () => {
    console.log("V4 server started.")
})


v2_server.listen(v2_conn_port, () => {
    console.log("V2 server started.")
})

and they do appear running as both logs are printed.

However, as I try to connect to the localhost on both ports, while I do see the html page provided, the socket.io connection doesn't seem to go through, as neither of the "Vx data received" logs appear (and buttons on the test page used to exchange data are unresponsive).

I'm pretty sure there might be a plethora of misplaced or misunderstood things here, as I'm just moving my first steps on node.js and web servers in general, so thanks in advance for the help and the patience!

CodePudding user response:

You cannot create two servers on the same HTTP port. However you can do something like this without attaching it to HTTP port:

const { Server } = require('socket.io');
const firstServer = new Server();
firstServer.listen(3000);

const secondServer = new Server();
firstServer.listen(4000);

CodePudding user response:

Alright, nevermind, turns out there was a really dumb error in the static HTML (forgot a to fix a typo in the test branch I was working on...).

So, everything is, unexpectedly, working flawlessly now!

For anyone that might arrive here and being intereseted in the code, I leave it down here (although be aware it's probably awful, but gets the job done).

https://github.com/hypothe/bomi_fama_nodejs

  •  Tags:  
  • Related