Express 4 + Socket.io = 404

Sven van den Boogaart picture Sven van den Boogaart · Mar 22, 2015 · Viewed 20.3k times · Source

For some hours ive been trying to get socket.io working with express 4. Ive started following this tutorial which gave me a working rest api.

To get socket.io working i used the command

npm install socket.io

This added socket.io to my node_modules dir. After this i've edited my app.js to contain the following code:

//LINE above var app = espress();
var http    =     require('http').Server(app);
var io      =     require("socket.io")(http);
io.on('connection',function(socket){  
    console.log("A user is connected");
});
// my routes etc

In my (jade) view i've added the following code :

script(src="https://cdn.socket.io/socket.io-1.2.0.js")
script.
    var socket = io('http://localhost/');

now when i run with npm start i get the following error in my browser and npm console:

http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1427056664743-51 404 (not found)

As far as i know i do not use express-generator as sugested in almost every answer at related question, what am i doing wrong?

my complete app.js

i've tried this : but the result is an error with port 3000 already in use.

Answer

Sven van den Boogaart picture Sven van den Boogaart · Mar 22, 2015

Solved it with thanks to the answer here

var app = express();
var http = require( "http" ).createServer( app );
var io = require( "socket.io" )( http );
http.listen(8080, "127.0.0.1");


io.on('connection',function(socket){  
    console.log("A user is connected");
});

At the client :

script(src='/javascripts/socket.io-1.2.0.js')
script.
    var socket = io.connect( "http://localhost:8080");

Changing ports did the trick (8080 instead of 3000)