Skip to content

Empty .to([]) sends to everybody instead of nobody #4645

Open

Description

@jyustman

Describe the bug
io.to([]).emit("hello") sends to everybody instead of nobody

To Reproduce

Socket.IO server version: 4.6.1

Server

import { Server } from "socket.io";

const io = new Server(3000, {});

io.on("connection", () => {
  io.to([]).emit("hello");
});

Socket.IO client version: 4.6.1

Client

import { io } from "socket.io-client";

const socket = io("ws://localhost:3000/", {});

socket.on("hello", () => {
  console.log("hello received");
});

Expected behavior
I expect no message to be sent when an empty array is passed to .to().

Platform:

  • OS: macOS 13.2

Additional context
It's easy to work around, but the behavior was unexpected.

Activity

  1. added
    to triageWaiting to be triaged by a member of the team
    on Mar 1, 2023
  2. myYang2016 commented on Mar 2, 2023

    @myYang2016

    I have this problem too, and I have to check every time to see if it's an empty array

  3. darrachequesne commented on Mar 3, 2023

    @darrachequesne
    Member

    @jyustman I could indeed reproduce the behavior, thanks. That is indeed surprising, I guess we can fix this in the next major release.

  4. added
    enhancementNew feature or request
    and removed
    to triageWaiting to be triaged by a member of the team
    on Mar 3, 2023
  5. added this to the 5.0.0 milestone on Mar 3, 2023
  6. inimaz commented on May 9, 2023

    @inimaz

    I can take a look at this one if that's fine?

  7. Frank3K commented on Dec 28, 2023

    @Frank3K
  8. drmgc commented on Mar 20, 2025

    @drmgc

    @Frank3K it is a in-memory adapter, and indeed it should be smth like if (rooms.size !== undefined). However, I have encountered same issue with a redis adapter.

    So, it must be fixed in multiple places

    UPD: Nevermind, the redis adapter still relies on the in-memory one

  9. added 3 commits that reference this issue on Mar 20, 2025
  10. darrachequesne commented on Mar 28, 2025

    @darrachequesne
    Member

    @drmgc the issue is that there is no distinction between no room and an empty room:

    Source:

    constructor(
    private readonly adapter: Adapter,
    private readonly rooms: Set<Room> = new Set<Room>(),
    private readonly exceptRooms: Set<Room> = new Set<Room>(),
    private readonly flags: BroadcastFlags & {
    expectSingleResponse?: boolean;
    } = {},
    ) {}

  11. drmgc commented on Mar 31, 2025

    @drmgc

    @darrachequesne yep, I found out already that no rooms is treated as no specific rooms.

    I thought it would be a simple one-line fix, but properly introducing a "broadcast to [] rooms" requires changes across quite a few places of this codebase, which I'm not familiar with tbh

  12. PrasanthGondi commented on May 10, 2025

    @PrasanthGondi

    Looks like this hasn't been fixed yet. When can we expect a fix for this?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions