Open chongma opened 3 years ago
Can the authors comment on this?? Problem -> On reconnecting, ws.close is called and then trying to connect or making a new web socket connection.
Isn't this ambiguous as a connection might still be open, and we might be opening a new one. Then server may send the close frame and onclose will be fired on client side, closing the new working connection !!
My problem turned out to be ping pong
@chongma Could you please elaborate on this? Did the old connection keep pinging the server even after closing?
@DamiToma The problem was on the server side. It wasn't correctly handling the ping pong. Eventually I moved to graphql subscriptions. So I didn't need to manually configure websockets any more.
@DamiToma The server code is something like this where it keeps a heartbeat unless the socket is closed
function heartbeat() {
this.isAlive = true;
}
function initialiseWebsocket(name) {
const wss = new WebSocketServer({ noServer: true });
wss.on('connection', function (ws) {
ws.isAlive = true;
ws.on('pong', heartbeat);
});
const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping();
});
}, 30000);
wss.on('close', function close() {
clearInterval(interval);
});
console.log(`Creating websocket ${name}`)
return wss
}
I am having a strange issue with websockets. I have my websocket server behind an apache httpd 2.4.51 proxy and the sockets keep disconnecting after 30 seconds or so. I am trying to investigate the issue but in the meantime I have installed reconnecting-websockets and it seems to work.
But now I have discovered that the chrome tab eventually runs out of memory and looking into the network tab on chrome reveals that lots of duplicate websockets are being created and they have status "101" whereas sometimes i get one that says status "finished"
I am using VueX to try to store a single instance of each reconnecting websocket, essentially like this
The issue may be related to the way that the server is terminating the connection but reconnecting-websockets shouldn't create a new one while the old one is still open?