If a socket leaves its default room, socket.to('room').emit messages will be received by it despite being sender
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 63.2k
- Forks
- 10.3k
- Avg merge
- 11d 20h
- Merged PRs (30d)
- 2
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided Socket.IO 4.5.3 server and client reproduction. Trace the room membership after socket.leave(socket.id) or socket.leaveAll(), then follow socket.to('TEST').emit and inspect socket.rooms. Done means the observed sender behavior is explained and the implementation or documented behavior is corrected consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend, networking
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100