Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ElXando opened this issue Nov 18, 2022 · 4 comments
Labels
enhancement New feature or request
Milestone

Comments

@ElXando
Copy link

ElXando commented Nov 18, 2022

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
@ElXando ElXando added the to triage Waiting to be triaged by a member of the team label Nov 18, 2022
@ElXando ElXando changed the title If a socket leaves its default room, socket.to('room').emit messages will be receives by it despite being sender If a socket leaves its default room, socket.to('room').emit messages will be received by it despite being sender Nov 18, 2022
@darrachequesne
Copy link
Member

Hi! I could indeed reproduce the issue.

Yes, that is a surprising behavior, we should definitely fix this in the next major version. Not sure how though.

@darrachequesne darrachequesne added enhancement New feature or request and removed to triage Waiting to be triaged by a member of the team labels Nov 30, 2022
@darrachequesne darrachequesne added this to the 5.0.0 milestone Nov 30, 2022
@0xamitr
Copy link

0xamitr commented Jan 27, 2024

Hey, Is this issue still not fixed?
I am facing the same issue in my code.

@darrachequesne
Copy link
Member

@0xamitr the behavior is still the same. For now, we strongly suggest not leaving the default room.

@hoeeeeeh
Copy link

When broadcasting, the room matching the sender's socket.id is typically excluded. However, if the sender leaves the default room, the room is removed, which can result in the sender not being excluded and still receiving messages. To address this issue, how about excluding the sender based on whether their ID exists in this.sids when a room matching their socket.id no longer exists?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants