Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome & Safari revert to xhr-polling rather than websockets with socket.io

Tags:

I have a problem with websockets and socket.io. When I try to connect to my node server with socket.io it initially connects using websockets but when reverts to jsonp-polling shortly after.

This is the output from the node sever when I connect:

8 Jun 07:01:15 - Initializing client with transport "websocket" 8 Jun 07:01:19 - Initializing client with transport "jsonp-polling" 8 Jun 07:01:19 - Client 16630339180119336 connected 

This happens in Chrome & Safari. I have updated to the latest socket.io version 0.6.17 and am running node 0.4.7.

I have tried deleteing my cookies and cache as suggested on github and SO, however the problem remains. Also, when I try to force websockets it never fully connects with a session ID.

Does anyone have any ideas?

like image 728
Kit Avatar asked Jun 08 '11 14:06

Kit


People also ask

How do I open up Chrome?

Whenever you want to open Chrome, just double-click the icon.

Is Google Chrome free to install?

Google Chrome is a fast web browser available at no charge. Before you download, you can check if Chrome supports your operating system and you have all the other system requirements.

Why should I install Chrome?

Security/PrivacyChrome keeps you safe and sound with its built-in malware and phishing protection. It has safe browsing technology and will show you a warning message before you visit a site that is suspicious. Chrome also automatically updates, so you always have the latest and most up-to-date version.

Is Chrome a better browser?

Chrome is ubiquitous — and for good reason. With a robust feature set, full Google Account integration, a thriving extension ecosystem (available through the Chrome Web Store), and a reliable suite of mobile apps, it's easy to see why Chrome is the most popular and the best web browser.


1 Answers

Websocket API is not supported by default in all the browsers at the moment (as per my knowledge) it should work on chromium though try testing it on chromium or firefox(after editing the default settings)and see if that still reverts to XHRPolling.

I am running it on a different IP as I need to run node on port 80 which causes conflict on my web server with Apache. Can websockets/flashsockets not be use cross-domain?

Now there might be 2 different reasons for the bug from here

  1. Web/Flash Sockets will not let u connect to the node.js client unless either u specify a differnt port like 81 or u specially specify apache to proxy the incoming request to Node. an easy solution could be writing the Node.js based HTTP server to just relay data from Apache (and setting Apache to run on a differnt port then 80)

    This link tells how to do that... in this process you can make Node.js do something like check if the request is from a websocket/httpbrowser if thats an http browser forward the request to Apache if not ie if thats from web/flash sockets then handle the socket accordingly. or as commented on the question. Specify APACHE to proxy to Node.js.

  2. Flashsockets require you to serve a crossdomain policy file on port 843 are you sure you are providing a cross domain file? (I think socket.io has inbuilt functionality to do that but still its always good to check.)

As told on the socket.io main website

In order to provide realtime connectivity on every browser, Socket.IO selects the most capable transport at runtime, without it affecting the API.

  • WebSocket
  • Adobe® Flash® Socket
  • AJAX long polling
  • AJAX multipart streaming
  • Forever Iframe
  • JSONP Polling

It's pretty clear that it will revert to AJAX Long Polling if websockets are disabled and Adobe Flash Socket fails to connect (this might be due to the unavailability of the policy file).

Here's a sample code for the cross domain file which you can include in your code and see if that makes your server run with websockets.

var net = require("net");  // Node.js   var Policy = net.createServer(function(socket) {     socket.setEncoding('utf8');     socket.on('connect',function(){         console.log("Policy Request");         socket.end("<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\"/></cross-domain-policy>");     }); });  Policy.listen(843);  
like image 116
ShrekOverflow Avatar answered Oct 05 '22 02:10

ShrekOverflow