2

I am new to node js and socket.io, i am trying basic example of socket.io in my windows machine.

Server Code

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

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="http://localhost:8080/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080/');

  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
  socket.on('connect', function () {
        alert('connect');
    });

    socket.on('error', function (data) {
        console.log(data || 'error');
    });

    socket.on('connect_failed', function (data) {
        console.log(data || 'connect_failed');
    });
</script>

In the above script client can not connect to the server (in console connect_failed was logged), but in the same time server side following was shown,

    info  - socket.io started
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized ALB0omsm3E2ZvPMn02x7
   debug - setting request GET /socket.io/1/websocket/ALB0omsm3E2ZvPMn02x7
   debug - set heartbeat interval for client ALB0omsm3E2ZvPMn02x7
   debug - client authorized for
   debug - websocket writing 1::
   debug - websocket writing 5:::{"name":"news","args":[{"hello":"world"}]}
   debug - setting request GET /socket.io/1/xhr-polling/ALB0omsm3E2ZvPMn02x7?t=1
374168523063
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared heartbeat interval for client ALB0omsm3E2ZvPMn02x7
   debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168533064&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - clearing poll timeout
   debug - clearing poll timeout
   debug - jsonppolling writing io.j[0]("8::");
   debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - jsonppolling closed due to exceeded duration
   debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168556555&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - clearing poll timeout
   debug - jsonppolling writing io.j[0]("8::");
   debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - jsonppolling closed due to exceeded duration
   debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168576586&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - clearing poll timeout
   debug - jsonppolling writing io.j[0]("8::");
   debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - jsonppolling closed due to exceeded duration
   debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168596600&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - clearing poll timeout
   debug - jsonppolling writing io.j[0]("8::");
   debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - jsonppolling closed due to exceeded duration
   debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168616640&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - clearing poll timeout
   debug - jsonppolling writing io.j[0]("8::");
   debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - jsonppolling closed due to exceeded duration
   debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168636656&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - setting request GET /socket.io/1/xhr-polling/ALB0omsm3E2ZvPMn02x7?t=1
374168523063
   debug - setting poll timeout
   debug - discarding transport
   debug - clearing poll timeout
   debug - clearing poll timeout
   debug - xhr-polling writing 8::
   debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - xhr-polling closed due to exceeded duration
   debug - setting request GET /socket.io/1/xhr-polling/ALB0omsm3E2ZvPMn02x7?t=1
374168663072
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - clearing poll timeout
   debug - xhr-polling writing 8::
   debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - xhr-polling closed due to exceeded duration
   debug - setting request GET /socket.io/1/xhr-polling/ALB0omsm3E2ZvPMn02x7?t=1
374168690890
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - clearing poll timeout
   debug - xhr-polling writing 8::
   debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
   debug - xhr-polling closed due to exceeded duration
   debug - setting request GET /socket.io/1/xhr-polling/ALB0omsm3E2ZvPMn02x7?t=1
374168710895
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7

How can i fix the error in the above scripts to successfully run the basic example of socket.io?

2 Answers 2

4

You can configure the socket.io settings as per your requirements. https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO

I also encountered the same problem and found that adding the below line solves the problem

io.set('transports',['xhr-polling']);
3
  • Hi. This line of code saves my life. I wonder why it works. Thanks!
    – ZillGate
    Nov 18, 2013 at 2:23
  • Does this mean it will only use xhr-pooling and not Websocket at all or when it is possible to use Websocket it will?
    – Maziyar
    Sep 22, 2014 at 15:00
  • Akamai does not support websockets. This is fall back. Feb 16, 2016 at 12:19
0

Make a localhost.js file with this data:

var port = 80,
http = require('http'),
fs = require('fs'),
socket = require('socket.io'),
app = function (req, res) {
  var url = req.url == '/' ? '/index.html' : req.url;
  fs.readFile(__dirname + url, 'utf8', function (err, data) {
    if (err) return console.log(err);
    res.end(data);
  });
},
httpServer = http.createServer(app).listen(port),
io = socket.listen(httpServer);
io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

And a index.html file with this:

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

  socket.on('connect', function () {
    console.log('connect');
  });

  socket.on('error', function (data) {
    console.log(data || 'error');
  });
</script>
</body>
</html>

put socket.min.js in the same folder then run in cmd:

node localhost.js
10
  • it does work for me ... im running nodejs on a windows 7. Maybe you should reinstall / update nodejs and socket.io. Jul 20, 2013 at 18:15
  • 1
    after am changing transport type to xhr, now working in my chrome and firefox :). Why? Jul 21, 2013 at 3:30
  • how did you changed to xhr? Jul 22, 2013 at 12:19
  • 1
    through io.set('transports',['xhr-polling']); Jul 22, 2013 at 16:14
  • with this setting you are not using sockets but ajax. that points toward some problem with your socket.io "installation" Jul 22, 2013 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.