2

I have three servers

  1. Main server - which listens to all HTTP request
  2. Socket Server 1 : Listen to X types of socket request
  3. Socket Server 2 : Listen to Y types of socket request

I want to run all three servers using same IP:Port combination and all three servers have different hostname. I have updated local DNS i.e. /etc/hosts to resolve all three domain names to same IP, take 127.0.0.1 for instance.

I am using VHost to resolve the requests on basis of hostname and directing request to appropriate server. Here is the code for that :

var express = require('express');
var vhost = require('vhost');
var app = express();
var https = require('https');


var mainServer =  <hostname1> ;
var socketServer1 = <hostname2> ;
var socketServer2 = <hostname3>;


var server = https.createServer(<options>, app);

server.listen(<somePort>,function(){
    console.log('VHost running');
});

var mainApp = require('./app.js');
var socketServerApp1 = require('./socketServer1');
var socketServerApp2 = require('./socketServer2');

app.use(vhost(mainServer, mainApp));
app.use(vhost(socketServer1, socketServerApp1(server)));
app.use(vhost(socketServer2, socketServerApp2(server)));

The problem I am facing in above code is, if I try to run both socket servers, the browser throws an error like :

WebSocket connection to 'wss://socketServer1:8443/socket.io/?EIO=3&transport=websocket&sid=qlSXz3os5eDI8c_EAAAC' failed: Connection closed before receiving a handshake response

But if there is only one socket server running at a time, then things works fine for that socket server i.e. if I comment one of app.use like :

app.use(vhost(mainServer, mainApp));
app.use(vhost(socketServer1, socketServerApp1(server)));
//  app.use(vhost(socketServer1, socketServerApp2(server)));

Is it not possible to run two socket servers on same IP:Port combination?

Aman Gupta
  • 3,627
  • 4
  • 40
  • 64
  • Sounds like a job for a proxy like nginx which could forward each incoming connection destined for a particular hostname to a different port on the one server. Then, you'd have three listeners, one for each, all listening on different ports. Other than that, you will likely have to simulate that same functionality on your own server by having one listener on the desired port that then spreads out the requests to the right handlers. – jfriend00 Feb 25 '16 at 07:29
  • is it necessary that you have the two different websocket servers? from that piece of code it seems like both listen to the same domain. Maybe `namespaces` in socket.io could solve your problem. – Gntem Feb 25 '16 at 07:33
  • What you are suggesting is to merge socketServer1 and socketServer2? – Aman Gupta Feb 25 '16 at 07:34
  • well yes you could merge and use namespaces, say you have one page `/realtime` or other pages below that `/realtime/deals` that emits other events and must be handled differently from the rest of the website. you can do in socket.io `.of('/realtime').on(,)` and so on, separating it this way from other common events and you dont need to have two different ws servers for the same application, i wont say that can't have it because apparently you can proxy requests as mentioned above, but in my opinion its simpler and more maintainable. – Gntem Feb 25 '16 at 07:50

1 Answers1

0

The problem was that both socket servers were having separate listners, which was causing the error. Moving socket listner to vhost App fixes the problem . Following is the code :

var express = require('express');
var vhost = require('vhost');
var app = express();
var https = require('https');
var socketio = require('socket.io');


var mainServer =  <hostname1> ;
var socketServer1 = <hostname2> ;
var socketServer2 = <hostname3>;


var server = https.createServer(<options>, app);
var io = socketio.listen(server);

server.listen(<somePort>,function(){
    console.log('VHost running');
});

var mainApp = require('./app.js');
var socketServerApp1 = require('./socketServer1');
var socketServerApp2 = require('./socketServer2');

app.use(vhost(mainServer, mainApp));
app.use(vhost(socketServer1, socketServerApp1(io)));
app.use(vhost(socketServer2, socketServerApp2(io)));
Aman Gupta
  • 3,627
  • 4
  • 40
  • 64