Issue with .to(room).emitWithAck()
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
I came across a bug where if you broadcast an event to a room, it times out unless you explicitly set a timeout variable.
To Reproduce
Please fill the following code example:
Socket.IO server version: ^4.0.0
Server
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { Server } from "socket.io";
const port = process.env.PORT || 3000;
const httpServer = createServer(async (req, res) => {
if (req.url !== "/") {
res.writeHead(404);
res.end("Not found");
return;
}
// reload the file every time
const content = await readFile("index.html");
res.writeHead(200, {
"Content-Type": "text/html",
"Content-Length": Buffer.byteLength(content),
});
res.end(content);
});
const io = new Server(httpServer, {});
const roomCode = "Test room code"
io.on("connection", (socket) => {
console.log(`connect ${socket.id}`);
socket.on("joinRoom", async (username, ack) => {
console.log(`${username} joining`);
socket.join(roomCode);
// This doesn't work
const response = await socket.to(roomCode).emitWithAck("userJoined", username);
// This does
// const response = await socket.to(roomCode).timeout(5000).emitWithAck("userJoined", username);
ack(response);
})
});
httpServer.listen(port, () => {
console.log(`server listening at http://localhost:${port}`);
});
Socket.IO client version: ^4.0.0
Client
import { io } from "socket.io-client";
const port = process.env.PORT || 3000;
const socket = io(`http://localhost:${port}`);
const socket2 = io(`http://localhost:${port}`);
socket.on("userJoined", (username, ack) => {
ack("username1")
})
const response1 = await socket.emitWithAck("joinRoom", "username1");
console.log(`Response 1 is ${response1}`);
const response2 = await socket2.emitWithAck("joinRoom", "username2")
console.log(`Response 2 is ${response2}`);
Expected behavior
It should work with or without the explicit timeout set.
Platform:
- OS: Ubuntu 22.04.5 LTS
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 supplied server and client reproduction, then inspect the implementation behind socket.to(roomCode).emitWithAck() and compare it with the explicit .timeout(5000) path. Add a regression test for a room broadcast with acknowledgements; done means the call resolves without requiring an explicit timeout.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js, typescript
- Domain
- api, backend, networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100