websockets / ws

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js
MIT License
21.34k stars 2.3k forks source link

Uhhh, what does .isAlive do again? #2217

Closed 439bananas closed 2 months ago

439bananas commented 2 months ago

Is there an existing issue for this?

Description

I have, as per the README, followed this code in order to close broken connections:

const interval = setInterval(function ping() { // Autoclose clients that are no longer alive
    wss.clients.forEach(function each(ws) {
        if (ws.isAlive === false) {
            return ws.terminate();
        }

        ws.isAlive = false;
        ws.ping();
    });
}, 30000);

turns out, it's not only closing broken connections, but it seems to be also closing connections that are currently open? i just got a notification from a webhook (that's triggered on the client side when the server closes the websocket) that implies the following as well?

ws version

8.16.0

Node.js Version

v20.11.1

System

System: OS: Windows 11 10.0.26085 CPU: (16) x64 AMD Ryzen 9 5900HX with Radeon Graphics Memory: 7.81 GB / 23.40 GB

Expected result

No response

Actual result

No response

Attachments

No response

lpinca commented 2 months ago

See the hearthbeat() function

function heartbeat() {
  this.isAlive = true;
}

It updates the flag every time a pong is received. Only unresponsive connections are closed.

lpinca commented 2 months ago

I'm closing this as answered.