nchan.js icon indicating copy to clipboard operation
nchan.js copied to clipboard

Secondary option to establish connection

Open rishiloyola opened this issue 8 years ago • 9 comments

Some old browsers do not support web sockets. In that scenario will this client automatically use the secondary option like long polling?

rishiloyola avatar May 12 '17 13:05 rishiloyola

Yes.

slact avatar May 12 '17 16:05 slact

There can be some errors while sending the data through web sockets. In that case, your client does not have a proper fallback mechanism. I disabled web sockets in my browser and tried out to make the connection. Your client throwing some errors instead of sending long polling requests.

rishiloyola avatar May 26 '17 19:05 rishiloyola

I need more information that this. What were the errors? How did you disable websockets? What browser are you using?

slact avatar May 27 '17 23:05 slact

I disabled WebSockets using some chrome extensions. As well as I also tried to make a connection with my Nginx server which has blocked web sockets configuration. In this scenario, nchan.js keep trying to make the connection rather than applying fallback mechanism. Errors : screen shot 2017-05-28 at 2 11 52 pm

Nginx Config

location ~ /subscribe/(.*) {
                nchan_subscriber 'longpoll';
                nchan_channel_id $1;
                add_header 'Access-Control-Allow-Headers' 'etag';
                add_header 'Access-Control-Allow-Credentials' 'true';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
}

nchan.js setup

 opt = {
          subscriber: ['websocket', 'longpoll'],
          reconnect: 'persist',
        }
wsSubscriber = new NchanSubscriber("http://localhost:9090/subscribe/12", opt);
      

rishiloyola avatar May 28 '17 08:05 rishiloyola

Okay, I see what's happening here. The fallback mechanism is just for browser-side support, not for server-side errors.

This is because there's no consistent way to tell if a 4xx response is due to a protocol being disallowed, or some other reason (like an upstream authentication request returning a 403). If you disable WS in chrome, Nchansubscriber should fallback to another protocol. If you get a handshake error, it will keep trying with the same protocol. You could handle that situation with an 'error' callback handler.

slact avatar May 30 '17 19:05 slact

I made some changes to get it work temporarily. [Code] What are your plans to fix this issue?

Thanks.

rishiloyola avatar May 30 '17 20:05 rishiloyola

I think it makes sense to try fall-back subscribers if the handshake fails. However, long-polling doesn't have a handshake, so I can't distinguish between a failed request and failed "handshake". I also don't think subscriber errors should automatically trigger a reconnect with a fallback.

So right now I'm not sure how to resolve this ambiguity. Maybe I could add a setting to cycle through falback subscribers on error, with infinite loop mitigation left up to the user. Your thoughts?

slact avatar May 30 '17 21:05 slact

Here are my changes to add fallback support in nchan.js. This is not the good way but it is working fine according to my needs. Link to code - https://github.com/slact/nchan.js/compare/master...interviewstreet:master

You are already reinitialising client whenever errors are taking place. I just added one parameter priorityIndex to iterate through configured subscriber array.

rishiloyola avatar Jun 28 '17 17:06 rishiloyola

Please open a pull request so that we can hash out these changes.

slact avatar Jun 30 '17 17:06 slact