45

For some reason my node server cannot serve the route /socket.io/socket.io.js, I always get a 404 error.
I tried compiling different node versions (current is 0.6.13 which also runs on server, where it actually works).
From the app.js I get info: socket.io started and no error when trying to call the socket.io.js.

I try it from localhost and port 8000 and I use the express framework

This is the code from app.js:

var express = require('express')
  , app = require('express').createServer()
  , io = require('socket.io').listen(app, { log: true });

app.listen(8000);

app.configure(function() {
    app.use(express.static(__dirname + '/public'));
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

io.sockets.on('connection', function (socket) {
   // all other stuff here
dan-lee
  • 14,365
  • 5
  • 52
  • 77

6 Answers6

95

Please check your Express version. Express recently is updated to 3.0alpha which API was changed. If 3.0 you can change your code to something likes this:

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

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

...

server.listen(8000);

Same issue with connect: https://github.com/senchalabs/connect/issues/500#issuecomment-4620773

nguyenkha
  • 1,356
  • 12
  • 13
  • Ok that sounds very close, I replaced it with your code and now I get this error when I try to start the server: `Cannot call method 'on' of undefined`. This corresponds to following code piece: `io.sockets.on('connection', function (socket) {`. This is just plain weird. I feel so lost. – dan-lee Apr 17 '12 at 13:38
  • btw I forgot to add that I actually have express 3.0alpha installed – dan-lee Apr 17 '12 at 13:45
  • Sorry, I write wrong code of socket.io. I updated it, please change your code again. – nguyenkha Apr 17 '12 at 13:48
  • No problem, it has just happened to me half a day ago :) – nguyenkha Apr 17 '12 at 13:58
  • 15
    For future reference don't forget to change `app.listen()` to `server.listen()` if you are using example snippets. – Diogo Gomes Mar 10 '13 at 16:36
  • I had tried probably 15 or 20 different variations of very similar code. This is the one that worked for me. Thanks! – pedalpete Jul 21 '13 at 01:55
  • This solution is viable with Docker too. Check [this](http://docker.lhsm.com.br/60aaaeec-d582-461b-8bd6-ebe2b63dfd50) post for an live example. –  Jan 04 '19 at 12:34
4

Using with the Express 3 web framework: (from socket.io)

> Express 3 requires that you instantiate a http.Server to attach socket.io to first:

meaning - (1) you must create a server instance:

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

(2) couple it with the socket.io:

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

and ONLY THEN - (3) make the server listen:

http.listen(8080);

make sure you keep this order!

Aviram Netanel
  • 12,633
  • 9
  • 45
  • 69
1

After installing node 0.8.1 I had the same problem. I just deleted the node_modules map in my project folder and reinstalled express/socket.io. After that it worked fine again with the code in your question.

roeland
  • 6,058
  • 7
  • 50
  • 67
0

Maybe this could help you, on my Ubuntu 11.10 I haven't properly set NODE_PATH variable, If you are on linux/mac try add line below to your .bashrc/.zshrc file.

export NODE_PATH=/usr/lib/node_modules:$NODE_PATH
Jacek Wysocki
  • 1,070
  • 11
  • 24
  • 1
    I added the `node_modules` to my NODE_PATH but it still doesn't work. Too bad, it really raised my hopes. But strangely enough, when installing something via npm it says: `Checking for node path: not found`. But when echoing $NODE_PATH I get `/usr/local/lib/node:/usr/local/lib/node_modules`. I am really confused at this point. – dan-lee Apr 17 '12 at 13:10
0

Install Socket.io inside your repository:

npm install socket.io --save 

After, config the server:

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

   server.listen(app.get('80')); // not 'app.listen'

And inside your archive HTML/EJS or another you want, add:

<script src="/socket.io/socket.io.js"></script>

Check if works with Console (Chrome/ Mozilla, etc).

In my example I use Chrome (Ctrl + shift + I):

enter image description here

Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53
0

The default port 3000 did not work for me, even though it is not in use. It fails with a 404 error for /socket.io/socket.io.js (and no other notification or explanation). Try a different port that you're sure is not in use, and it might work for you.

Peter Kionga-Kamau
  • 6,504
  • 2
  • 17
  • 13