slact / nchan.js

NPM package for the Javasript client for Nchan
Other
91 stars 25 forks source link

Secondary option to establish connection #4

Open rishiloyola opened 7 years ago

rishiloyola commented 7 years ago

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

slact commented 7 years ago

Yes.

rishiloyola commented 7 years ago

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.

slact commented 7 years ago

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

rishiloyola commented 7 years ago

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);
slact commented 7 years ago

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.

rishiloyola commented 7 years ago

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

Thanks.

slact commented 7 years ago

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?

rishiloyola commented 7 years ago

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.

slact commented 7 years ago

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