Skip to content

If a socket leaves its default room, socket.to('room').emit messages will be received by it despite being sender #4524

Open
@ElXando

Description

@ElXando

Describe the bug
If a socket leaves the default room which matches its socket id, socket.to('room').emit messages will be receives by the client of the sender, even though the documentation makes no mention of this.

To Reproduce

Please fill the following code example:

Socket.IO server version: 4.5.3

Server

import { Server } from "socket.io";

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

io.on("connection", (socket) => {
  socket.leave(socket.id);
  OR
  socket.leaveAll();

  socket.join('TEST');

  socket.on("test", (message) => {
    socket.to('TEST').emit('test', message);
  });
});

Socket.IO client version: 4.5.3

Client

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

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

socket.on("test", (message) => {
  console.log(message);
  // Is logged by sender as well as other clients in the room
});

socket.emit('test', 'hello');

Expected behavior
I would not expect that leaving the default room would mean the sender receives a message that otherwise it shouldn't. My idea of leaving the default room is that I can ensure each connected client is only in a single room, and then when emitting to the room they're in I was hoping to use:

const [thisRoom] = socket.rooms;
socket.to(thisRoom).emit('test', message);

Perhaps there is a different expected way to use rooms but it would seem a bit clunky to use const [, second] = socket.rooms if I leave each client in the default room?

Platform:

  • Device: Windows
  • OS: Server 2019 Server, 11 Client

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions