How do you run express and socket.io on the same port

its always annoying, i have to always start listening on the new port in my app.js, that is, for example i can access my site from localhost: 3000 and localhost: 4000 and socket.io on my site only works using localhost : 4000. I just pushed him to hero and realized that I can't change the port to hero

this is what i have

app.js:

var express = require("express");
var io = require('socket.io')
var port = 3000;

var app = express()
  , server = require('http').createServer(app)
  , io = io.listen(server);

server.listen(3000);

module.exports = io;

      

client.js:

var socket = io('http://localhost/');

      

+3


source to share


2 answers


I can do what you want:



var exp = express();
var server = require('http').Server(exp)
var io = require('socket.io')(server);

//setup stuff

server.listen(3000)

      

+3


source


Simplest version of express 4 + socket.io 1.0 (which I use myself):

var express = require('express');
var app = express();
var server = app.listen(3000, function() {
    console.log("server started on port 3000");
});

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

      



I honestly don't know why your version is not working, but I advised several people to follow this sequence (which I got from the documentation) and it worked for me and for them. This is likely due to the fact that it is io

properly connected to Express 4 and is not using a shared http

module as well express

.

+1


source







All Articles