Socket.IO defaults to xhr-polling?

3,707 views
Skip to first unread message

Pedro Teixeira

unread,
Nov 5, 2010, 7:06:53 AM11/5/10
to Socket.IO
I'm using Socket.IO and Socket.IO-node 0.6 (installed via npm) and,
when initializing a socket on the Chrome browser (Chrome version 7.0),
I get degraded to "xhr-polling", even though Chrome has websockets.
Any ideas on why this is happening?

Here is my client code:


socket = new io.Socket('localhost', {port: 4000});
socket.connect();
console.log('connecting...');
socket.on('connect', function() {
console.log('connected');
});


Here is my server code:


var http = require('http'),
sys = require('sys'),
fs = require('fs'),
io = require('socket.io');

var clients = [];

var server = http.createServer(function(request, response) {
response.writeHead(200, {
'Content-Type': 'text/html'
});

var rs = fs.createReadStream(__dirname + '/template.html');
sys.pump(rs, response);

});

var socket = io.listen(server);
socket.on('connection', function(client) {

var username;

clients.push(client);

client.send('Welcome to this chat server!');
client.send('Please input your username:');

client.on('message', function(message) {
if (!username) {
username = message.toString();
client.send('Welcome, ' + username + '!');
return;
}
var broadcast_message = username + ' said: ' + message.toString();
socket.broadcast(broadcast_message);
});

client.on('disconnect', function() {
var pos = clients.indexOf(client);
if (pos >= 0) {
clients.splice(pos, 1);
}
});

});

server.listen(4000);

Shripad K

unread,
Nov 5, 2010, 4:03:32 PM11/5/10
to sock...@googlegroups.com
Clean up your browser cookies. And try again. That might work.

Pedro Teixeira

unread,
Nov 6, 2010, 6:43:40 PM11/6/10
to Socket.IO
Worked like a charm :)
I'm curious, what may be the reason?
This looks like a bug on feature detection, no?

Thanks!

On Nov 5, 8:03 pm, Shripad K <assortmentofso...@gmail.com> wrote:
> Clean up your browser cookies. And try again. That might work.
>

Eric S.

unread,
Dec 17, 2010, 1:18:53 AM12/17/10
to Socket.IO
I have a similar problem.

If I clear my cookies, I get an initial 'websocket' connection from
Chrome 8, but it is quickly closed and a polling connection is
opened. Plus, a cookie is set so that on page reload, polling is used
right off the bat.

Here's a snippet of the output from node:

17 Dec 00:15:48 - Initializing client with transport "websocket"
17 Dec 00:15:48 - Client 8825012515299022 connected
17 Dec 00:15:48 - Client 8825012515299022 disconnected
17 Dec 00:15:53 - Initializing client with transport "xhr-polling"
17 Dec 00:15:53 - Client 36253320169635117 connected

Any ideas?

Shripad K

unread,
Dec 17, 2010, 1:25:27 AM12/17/10
to sock...@googlegroups.com
Yes. Socket.IO has a rememberTransport option that basically stores a cookie with the transport that it chose to use initially. And on page reload it just loads the transport that was stored in the cookie rather than try feature detection again. To prevent this behaviour set rememberTransport: false. However, Eric it seems you are on master branch. I had a similar issue. You can checkout 0.6.2 which is stable. The master branch is experimental and may have bugs.

Eric S.

unread,
Dec 21, 2010, 2:12:23 PM12/21/10
to sock...@googlegroups.com
I discovered that in my case, the problem is with my host: WebFaction. They put everything behind nginx, which mangles the websocket headers and causes websocket to fail, so Chrome correctly falls back on xhr-polling.

If I pay for a static IP, WebFaction will open a port directly to node, and (if I configure my socket.io client to use that port) Chrome should be able to use websocket correctly.

Shannon -jj Behrens

unread,
Dec 21, 2010, 3:12:00 PM12/21/10
to sock...@googlegroups.com

You might want to check out the prices on RimuHosting. A Linux VPS
would give you more control.

-jj

--
In this life we cannot do great things. We can only do small things
with great love. -- Mother Teresa
http://jjinux.blogspot.com/

Reply all
Reply to author
Forward
0 new messages