Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "xhr-polling" config do in socket.io?

I have a node.js server with socket.io:

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

// assuming io is the Socket.IO server object
io.configure(function () { 
      io.set("transports", ["xhr-polling"]); 
      io.set("polling duration", 10); 
    });

io.sockets.on('connection', function(socket){
  console.log('connected: %s', socket.id);
  ...
}

With xhr-polling and a polling duration of 10 seconds, does this mean that a new connection will be invoked every 10 seconds? If so, how can I keep track of users if they keep disconnecting? I'm running node.js on heroku.

like image 699
Michael Eilers Smith Avatar asked Feb 11 '12 21:02

Michael Eilers Smith


2 Answers

xhr-polling means that your server will be waiting for 10 seconds on any GET of POST received that it does not have an answer before answering instead of sending back an empty response. So, if your server has no information to give back after 10 seconds, it will answer back with an empty response. You can read more here : Long polling

I personally use xhr-polling as a fallback option from WebSockets in an application on nodejitsu (another node hosting like Heroku) and it is working fine. The only thing is the "on connection" event that takes about 3-8 seconds instead of being instant as with my WebSocket application.

There is no new connection that is created on every new polling, it is just a way that only one GET or POST is sent to the server every 10 seconds, instead of having to poll the server every .5 seconds to have a "real-time" application. If the server answer in less than 10 seconds, there will be another poll sent to prepare the next answer.

I hope this will help you. Have a good day.

like image 57
Bruno Gagnon-Adam Avatar answered Oct 08 '22 07:10

Bruno Gagnon-Adam


socket.io reconnects clients automatically.

like image 22
Milan Babuškov Avatar answered Oct 08 '22 07:10

Milan Babuškov