Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can WebSockets (or AJAX long-polling, etc.) run on a different port than the original HTTP request?

I want to write an application that uses WebSockets with node.js and socket.io in combination with Django and will be sharing data through Redis (lots of buzzwords in that sentence!).

If I run node on a different port (e.g., 5555), will I be able to connect to it with a WebSocket (or fallback method with socket.io, like long-polling, etc.) or will the same origin policy give me problems?

If so, how can I get around them?

like image 944
Ankit Avatar asked Mar 19 '11 12:03

Ankit


2 Answers

Socket.io supports JSONP polling which is commonly used for cross-domain long polling (apart from CORS which is not supported by every browser). On cross-domain subject regarding WebSockets - specification states the following:

The server includes the hostname in the |Sec-WebSocket-Location| field of its handshake, so that both the client and the server can verify that they agree on which host is in use.

You "should" be able to use WebSockets with different port, in fact you probably have no other choice since standard ports like 80 will be used by other web server to serve your Django based application. There will be also potential problems with browser specific parallel connection limits if you use the same domain and port (for example you can see this in Facebook chat when you try to open more than one tab within the same browser context - each tab is connected to different subdomain during long polling to overcome these limits).

like image 60
yojimbo87 Avatar answered Sep 21 '22 03:09

yojimbo87


The answer is "sometimes". Its very much browser and security policy specific.

Assuming you're using Socket.IO... I've had no issues, except in Opera where it was unable to bypass the SOP (Same-origin policy) rules. The fix was enabling WebSockets.

like image 23
tbranyen Avatar answered Sep 22 '22 03:09

tbranyen