faye/faye — pull request #134 by morr (2012-04-06 15:10:00+00:00)
Title: separate endpoint for concrete transport

##### Usage

javascript:

var client = new Faye.Client('http://'+location.hostname+'/faye', {
transportEndpoints: {
websocket: 'http://'+location.hostname+':9292/faye'
}
});

nginx.conf:

server {
listen 80;
server_name localhost;

location /faye {
proxy_pass http://localhost:9292;
}
}

##### Synopsis
Hello,
I tried to integrate Faye with my web site and stumbled upon an issue for which I didn't find any reliable solution in internet.

Here is the problem:
My site is located at http://mysite.org and faye-endpoint is placed at http://mysite.org:9292/faye
mysite.org and mysite.org:9292 are different domains so Faye can't use AJAX protocol, instead it choses JSONP. But JSONP request causes wait cursor in opera (https://github.com/faye/faye/issues/114) and sometimes i see it in my google chrome.

Okay, lets get rid of different domains and stream all http://mysite.org/faye requests on nginx layer into http://mysite.org:9292/faye

nginx.conf:

server {
listen 80;
server_name localhost;

location /faye {
proxy_pass http://localhost:9292;
}
}

Yeeeah, it works. Almost. Now we have broken websokets.
It happened because nginx can't proxy_pass websocket connections. Even this module https://github.com/yaoweibin/nginx_tcp_proxy_module can't help(it can stream from a separate domain only(or I just didn't find out how to do opposite))

But why should it be a problem? We don't have to use the same endpoint for websockets, don't we?
So I looked at faye-browser.js and found function

getSocketUrl: function(endpoint) {
if (Faye.URI) endpoint = Faye.URI.parse(endpoint).toURL();
return endpoint.replace(/^http(s?):/ig, 'ws$1:');
},

I changed it for

getSocketUrl: function(endpoint) {
if (Faye.URI) endpoint = Faye.URI.parse(endpoint).toURL();
return endpoint.replace(/^http(s?):/ig, 'ws$1:').replace(':80', ':9292');
},

And now websockets works perfectly!

So I refactored this fix into optional hash parameter for Faye.Client constructor options argument.