Question

I installed nodejs and socket.io, and when I run the demo code at http://socket.io/#how-to-use, I always get this on server:

info  - socket.io started
debug - served static content /socket.io.js

On my browser, nothing output. It seems not working well. I use Chrome 16 beta.

I have a nginx installed on my server, too. So I changed the socket.io listening port to 8001.

Server code:

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

app.listen(8001);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

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

Client code:

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

Solution

Code works for me, can you try this in your client code:

var socket = io.connect(document.location.href);

to make sure that the URL is 100% accurate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top