Description
Describe the bug
When the retries option is enabled in the Socket.IO client, the client emits the initial event twice inside the 'connect' handler. However, this issue does not occur when the retries option is not provided.
To Reproduce
Please fill the following code example:
Socket.IO server version: 4.8.1
Server
const io = new Server(httpServer, {});
io.on("connection", (socket) => {
console.log(`connect ${socket.id}`);
socket.on('data', (data, callback)=> {
console.log('Incoming message from client: ', data);
callback(null, {
success: true,
message: 'Acknowledged...'
});
});
socket.on("disconnect", (reason) => {
console.log(`disconnect ${socket.id} due to ${reason}`);
});
});
Socket.IO client version: 4.8.1
Client
const socket = io(`http://localhost:${port}`, {
retries: 2,
ackTimeout: 3000,
transports: ["websocket"],
});
socket.on("connect", () => {
console.log(`connect ${socket.id}`);
const msg1 = 'Hello!!!';
const msg2 = 'Byeee';
socket.emit('data', msg1);
socket.emit('data', msg2);
console.log('Emitted msg: ', msg1);
console.log('Emitted msg: ', msg2);
});
Expected behavior
The event inside the 'connect' handler should be emitted only once, regardless of whether the retries option is enabled or disabled.
Platform:
- Device: Windows/google chrome
- OS: Windows/google chrome
Additional context
Console output:
I debugged this issue and it seems like:
After the client emits the connect event, the event queue is drained with the force option set to true. This action retriggers events that were already added to the queue & awaiting acknowledgement due to the retries option. As a result, the initial event is emitted twice.
Activity
darrachequesne commentedon Dec 21, 2024
Hi! I think that's because the client expects a
null
as a first argument for the callback:Source
socket.io/packages/socket.io-client/lib/socket.ts
Line 548 in 7427109
It seems it's not properly documented though...
backendsuraj commentedon Dec 29, 2024
Hi @darrachequesne
I reran this with null as a first argument but still same result.