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.
ws.ping(null,null,false)
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
Do note, I have tried
@humingchun https://github.com/websockets/ws/issues/459
ws._socket.setKeepAlive(true,100)
Also, one fork by @FlorianBELLAZOUZ
https://github.com/FlorianBELLAZOUZ/ws
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,
var ws=require("ws");
var wsx;
var init=function() {
wsx=new ws("ws://example.com");
wsx.on("close",function() {
console.log("try to reconnect");
init();
};
}
init();
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: error is not emitted if ws.terminate() is called, but close is.
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.
Most helpful comment
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.