Skip to content

Leave multiple rooms at once #5391

Description

@ZachHaber

Is your feature request related to a problem? Please describe.
In my functions to cause a socket to manage leave all rooms they no longer have permissions for and join rooms they now have permissions for, there's no way to leave multiple rooms at once without individually leaving each room, which seems like a waste, especially if I end up moving to an adapter that makes leaving async.

Currently the JSDocs for the socket leave mentions that you can use socket.leave('room1').leave('room2') which isn't valid according to the typescript as it returns Promise<void> | void.

Which means that to leave multiple rooms I have to do something like this depending on the behavior I'm looking for.

const toLeave = new Set<string>();
// ... fill toLeave
for(const room of toLeave){
  await socket.leave(room);
}
await Promise.all(Array.from(toLeave,room=>socket.leave(room)));

Describe the solution you'd like
I'd like leave to have the same functionality as join where it allows a single value or an array of values: socket.leave(['room1','room2'])

Which would make the above example much simpler and presumably allow the adapters to handle it more efficiently.

await socket.leave(Array.from(toLeave));

Describe alternatives you've considered
I'm not sure what other alternatives exist. Regardless, fixing the JSDocs or the typescript types - based on which one of those is wrong would be helpful (which I'm pretty sure is the JSDoc)

Activity

  1. AnthoMC27 commented on Sep 16, 2025

    @AnthoMC27

    Creo que pudiese hacer algo cómo una función que recorra todas las Rooms, y la clase sería io.leave(), si se deja vacío el parámetro entonces sales de todas las rooms, aunque igual tendrías que recorrer los namespaces para cerrar las rooms de los mismos, aunque bajo el mismo concepto se puede hacer que averigüe primero los namespaces y luego cierre las rooms.

  2. imnotniwa commented on Sep 17, 2025

    @imnotniwa

    I think you can use io.in(socketId).socketsLeave(['room1',room2']) instead of socket.leave()
    #ref

  3. darrachequesne commented on Sep 24, 2025

    @darrachequesne
    Member

    Hi!

    That is indeed an oversight from #2879. Let's fix this.

  4. ZachHaber commented on Oct 1, 2025

    @ZachHaber
    ContributorAuthor

    If no-one has started work on this in 2ish weeks (when I get back to a computer), I'll work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions