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

Client emits initial event twice inside 'connect' handler when retries are enabled. #5258

Open
backendsuraj opened this issue Dec 12, 2024 · 1 comment · May be fixed by #5259
Open

Client emits initial event twice inside 'connect' handler when retries are enabled. #5258

backendsuraj opened this issue Dec 12, 2024 · 1 comment · May be fixed by #5259
Labels
to triage Waiting to be triaged by a member of the team

Comments

@backendsuraj
Copy link

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({
      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:
image

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.

image

@backendsuraj backendsuraj added the to triage Waiting to be triaged by a member of the team label Dec 12, 2024
@backendsuraj backendsuraj linked a pull request Dec 12, 2024 that will close this issue
5 tasks
@darrachequesne
Copy link
Member

Hi! I think that's because the client expects a null as a first argument for the callback:

io.on("connection", (socket) => {
  // [...]
  socket.on('data', (data, callback)=> {
    console.log('Incoming message from client: ', data);
    callback(null, {
      success: true,
      message: 'Acknowledged...'
    });
  });
  // [...]
});

Source

const hasError = err !== null;

It seems it's not properly documented though...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to triage Waiting to be triaged by a member of the team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants