Arun Kumar Arun Kumar - 3 months ago 34
Node.js Question

How to start node express, binaryserver and socket.io on same port?

I have code snippet to explain what i am doing and what i want.

var express = require('express');
var http = require('http');
var app = express();

app.use('/', express.static(__dirname + '/static'));

var BinaryServer = require('binaryjs').BinaryServer;
var server = http.createServer(app);

var binaryServer = new BinaryServer({server:server});


var ioServer = http.createServer(app);

var io = require('socket.io').listen(ioServer);


I can run node express and socket.io on same port.

ioServer.listen(8080, function(){
console.log('server running at localhost:8080');
});


Same can be done with node express and binaryServer.

server.listen(8080, function(){
console.log('server running at localhost:8080');
});


But i want to run node express, socket.io and binaryServer on same port express is running (8080 in this case).
Any suggestions ?

Answer

You would need to attach both the SocketIO and binaryServer to same http server instance then bring that single instance up.

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var binaryServer = new BinaryServer({ server:server, path: '/binary'});

server.listen(8080, function(){
  console.log('http/socket/binary server running at localhost:8080');
});

Set the path so binaryServer doesn't conflict with any of your apps. This path is required in the client connections too.