ws: Websocket does not terminate all connections when calling .close() method

Is there an existing issue for this?

  • I’ve searched for any related issues and avoided creating a duplicate issue.

Description

I’m using version 8.13.0

I dont exactly know where the issue is, but the following code resulted having hanging/zombie connections on the client side. (they never received a clsoed event from the sever).

The server code:

    websocket.close();
    server.close();

Note that the websocket has many clients, not only one. (around 5-10 clients in my case) This resulted zombie clients, not receiving the disconnection/close event.

The code that fixed my issue:

    websocket.clients.forEach((a) => {
      a.terminate();
      a.close();
    });

    websocket.close();
    server.close();
    ```

### ws version

8.13.0

### Node.js Version

16.14.2

### System

_No response_

### Expected result

All connected clients automatically disconnected

### Actual result

Clients still have live connection (seemingly)

### Attachments

_No response_

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

@simplecommerce the 'close' event is emitted if clientTracking is enabled

import { WebSocketServer } from 'ws';

const websocketServer = new WebSocketServer({ noServer: true });

websocketServer.on('close', function () {
  console.log('close');
});

websocketServer.close();