-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Reconnection and KeepAlive #767
Comments
Do note, I have tried
Also, one fork by @FlorianBELLAZOUZ |
Are there any plans to add a keepAlive feature ? The reason I ask is almost everyone using this lib must be implementing their own workaround. |
@john-doherty yeah an heartbeat system based on server sent pings makes sense. Not before 2.0.0 is released though. |
anyone can tell me how to reconnect this socket? i try using like w3c socket,
but it not worked, no callback after reconnect |
I've the exact same problem! I had to implement my own reconnect handling but more the WebSocket doesn't recognize a connection termination which is not caused by the server (client connection/internet issues). This is really a big problem here :( You can easily debug/test that by connecting to a WS Server and then pulling the LAN cable |
First you could use ping/pong and if no pong is returned within X time assume it failed no? Also I find on connection issues (with ping/pong) the ready state of the socket will change so reconnecting then. it would be nice if there was a reconnect or "open" call rather than having to create a new web socket (but fairly minor). |
As @michaelsanford said you can use something like this: https://github.com/websockets/ws#how-to-detect-and-close-broken-connections. Reconnection can be handled with one of the many modules available for this on npm. Closing, feel free to continue discussing on the closed thread. |
Sorry for necro. Do i take it that there is still no auto-reconnect feature, @lpinca ? The link you usggested above is when you implement both client and server. I connect to a public wss:// server which doesn't know ping/pong'ing. I just want to continuously try to reconnect when the connection drops, regardless of why it dropped. I look for a simple but reliable wrapper code to implement reconnection but didn't find much. I implemented my own, with timeout too, inspired from https://github.com/joewalnes/reconnecting-websocket/blob/master/reconnecting-websocket.js, and I'm leaving it below - please do feel free to point out if it can be made more reliable. var timeout = 10; // seconds
function connect(address, protocols, options) {
let ws = new WebSocket(address, protocols, options);
let timerTimeout = setTimeout(() => ws.terminate(), timeout * 1000); // force close unless cleared on 'open'
ws.on('open', () => {
console.log('Opened. Clearing timeout ...');
clearTimeout(timerTimeout);
// do your thing here, like ws.send(...);
});
ws.on('message', data => console.log(data.slice(0, 76)+' ...'));
ws.on('close', () => {
clearTimeout(timerTimeout);
console.error('Websocket connection closed. Reconnecting in %f seconds ...', timeout);
setTimeout(() => connect(address, protocols, options), timeout * 1000);
});
ws.on('error', reason => console.error('Websocket error: ' + reason.toString()));
return ws;
} Note to self: |
That's correct, there is no auto reconnect. |
@lpinca Any chances of this auto-reconnect feature getting implemented? |
@aleqx @royalpinto to fire onclose event on client side when its connection is broken (e.g. tested with unplugging LAN cable or router restart) you can implement heartbeat mechanism recommended by @lpinca: https://github.com/websockets/ws#how-to-detect-and-close-broken-connections. But it is depends on pings from server. If you can not modify target server to send pings to clients you can send ping from your client to server and you do not need to listen for ping (maybe server do not support ping/pong) because sending ping (or any data) causes that ws client notices broken connection and fires onclose event. In onclose you should already have implemented own reconnect mechanism. Note that broken connection is not noticed immediately after ws.ping() or ws.send() but it takes some time (e.g. in my case on windows I was sending ping each 5 seconds and after LAN cable unplugging "onclose" was fired after 3rd ping (in about 15 seconds). On raspberian (maybe any Unix?) I notices that onclose is fired after unplugging + plugging again but it also acceptable because reconnection happens on connection restored. Auto reconnect depends on many variables and differs from use-case to use-case so it should be tailored by your needs. |
Actually the WebSocket connection is closed by underlying HTTP session if you are using Nginx or some other reverse proxy. WebSocket traffic and keep-alive(ping/pong) won't affect underlying HTTP (reverse proxy) anymore after your protocol upgraded to WebSocket, so HTTP connection will timeout eventually due to no any HTTP traffic.
to your Nginx location directive, and you can keep the underlying HTTP and WebSocket alive for 30 days. |
Unfortunately, I am using Cloudflare, and can't keep underlying HTTP alive, it'll timeout every 100 seconds. So an API which can interactive with underlying HTTP connection may help... |
@aleqx |
@adrian-gierakowski you are right, quoted reconnect implementation is wrong. It says that it is inspired by https://github.com/joewalnes/reconnecting-websocket/blob/master/reconnecting-websocket.js but that implementation is OK because each (re)connection is always stored in |
I still think that reconnect mechanism should not be part of library but it could be fine @lpinca to add to doc some professional/good practice reconnect example such as we have example for heartbeat mechanism for detecting zombie connections. |
My end requirement is to be connected. So if i get disconnected, I must connect back.
To do this the biggest challenge I am facing is that neither the close event is called, not are my sends failing, neither is ping pong throwing error.
I added a ping pong in the background to keepAlive the connection.
However that didnt also report close events. As that itself stopped responding when I pulled off the internet from the router.
My end goal is to remain connected. Please suggest how to do that
The text was updated successfully, but these errors were encountered: