1

I'm trying to simulate on one machine multiple servers that communicate with socket.io in a node.js program. There is going to be one coordinator server, and many client servers. I'm doing this with the future goal of having a "web service program" for which I increase it's processing capability by adding more servers.

here is a simplification of the coordinator server part of the code;

const httpM= require('http');
const socketIo = require('socket.io');
const express = require('express');
const app = express();

var http = httpM.Server(app);
 io = socketIo(http);
 io.on('connection', function(socket) {
             console.log("successful connection");
             socket.on("test",function(msg){
                 console.log(msg);
             })
        });
const PORT = process.env.PORT || (5000);
http.listen(PORT, () => console.log(`coordinator server: started on ${PORT}`));

here is the client server part.

const clientIo = require("socket.io/client-dist/socket.io");
var interServerSocket;
setInterval(function() {
      if(interServerSocket==null||interServerSocket==undefined)
      {
        interServerSocket= clientIo.connect("http://localhost:5000");
        console.log("-should have connected the interserver socket");
      }
      interServerSocket.emit("test","hello coordinator here to serve");
      console.log("-should have emit");
    },5000);

But my code doesn't work. I never get "successful connection" logged in the console nor any of the messages I emit logged. Nothing is logged from the coordinator server part. I don't see what I could be doing wrong. I was thinking it could be something to do with the fact that the code is all in the same node program in the same machine. Maybe I'm writing the url wrong. I appreciate all help.

I found this question that helped me: Is it possible to set up a socket.io client running (server-side) on a node.js server? I think it's outdated though, require("socket.io-client"); needs to be changed with require("socket.io/client-dist/socket.io");. In any case for my case for some reason it's not working.

Cavern head
  • 21
  • 1
  • 7

1 Answers1

0

answer in short download socket.io-client with the command npm i socket.io-client then use require("socket.io-client"); intead of require("socket.io/client-dist/socket.io");

The answer was in this thread after all: Is it possible to set up a socket.io client running (server-side) on a node.js server? I was wrong socket.io-client is actually another npm module that one can download separate from socket.io: https://www.npmjs.com/package/socket.io-client using: require("socket.io/client-dist/socket.io"); unexpectedly gave me no compilation errors but it seems it wasn't the right way to do this the socket.io/client-dist/socket.io module is maybe only for the browser or something.

Cavern head
  • 21
  • 1
  • 7