0

Possible Duplicate:
socket.io: Failed to load resource

A simple express + socket.io hellow world app seems doesnt work, i keep getting

"NetworkError: 404 Not Found - http:// localhost:3002/socket.io/socket.io.js"

Anyone know this issue?

code:

app.js

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

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


app.configure(function () {
app.use(express.static(__dirname + '/public'));
});

app.listen(3002);


io.sockets.on('connection', function(socket) {
socket.emit('news', {hello: 'world'});
socket.on('my other event', function (data) {
    console.log(data);
});
});

index.html

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my:'data' });
});

Community
  • 1
  • 1
nihulus
  • 1,475
  • 4
  • 24
  • 46
  • ["The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with the server, but the server could not find what was requested."](http://en.wikipedia.org/wiki/HTTP_404) – Engineer Jul 26 '12 at 07:49
  • Yes, but whats wrong with the code that cause the issue? – nihulus Jul 26 '12 at 08:00

1 Answers1

1

Your Socket.IO server is listening on port 3002, but you connected to:

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

which by default uses standard port 80. Try this:

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

Also you will probably have to change localhost to the IP of your machine.

Side note: If you are running Express Server and Socket.IO server on the same port, then you can simply do:

var socket = io.connect();
freakish
  • 54,167
  • 9
  • 132
  • 169
  • Thanks for reply sir! I dont think theres a different between using localhost or my IP? Alose i've tried your fix, still getting 404. – nihulus Jul 26 '12 at 08:12
  • That's interesting. Maybe this is because you fired server listening after socket listening? Put this line: `var io = require('socket.io').listen(server);` after `app.listen(3002);`. That's the last idea I have. :) – freakish Jul 26 '12 at 08:40
  • Make sure you aren't reloading a cached page with the old portless URL. – ebohlman Jul 26 '12 at 11:40