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

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

Open jyustman opened this issue 2 years ago • 8 comments

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.

jyustman avatar Mar 01 '23 15:03 jyustman

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

myYang2016 avatar Mar 02 '23 04:03 myYang2016

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

darrachequesne avatar Mar 03 '23 09:03 darrachequesne

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

inimaz avatar May 09 '23 17:05 inimaz

I observed the same. Could it be due to the following condition in socket.io-adapter?

https://github.com/socketio/socket.io-adapter/blob/28f60b880ad04ff4b48ade0cd529f1970643604f/lib/index.ts#L335-L355

Frank3K avatar Dec 28 '23 08:12 Frank3K