socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

fix(socket.io): improve `close()` function

Open fantua opened this issue 7 months ago • 1 comments

The kind of change this PR does introduce

  • [x] a bug fix
  • [ ] a new feature
  • [ ] an update to the documentation
  • [ ] a code change that improves performance
  • [ ] other

Current behavior

After https://github.com/socketio/socket.io/pull/4971 the usage of the close() function has become confusing and sometimes misleading.

Since close() now returns a Promise, I would expect the httpServer to close when the promise is resolved. However, this is not the case.

await io.close();
// httpServer not closed yet

Also, in order to catch all possible errors, I need to use a mixed style of async/await and callbacks:

io.close(error => {
  if (error) {
    // error here
  }
}).catch(error => {
  // another error here
});

New behavior

await io.close();
// httpServer closed
io.close().catch(error => {
  // all errors here
});

fantua avatar Apr 30 '25 14:04 fantua

@darrachequesne could you please take a look? Thanks

fantua avatar May 14 '25 14:05 fantua

I think it's good moment to deprecate the error argument here:

https://github.com/socketio/socket.io/blob/91e1c8b3584054db6072046404a24e79a17c1367/packages/socket.io/lib/index.ts#L807

piotr-cz avatar Oct 23 '25 09:10 piotr-cz