AndyGura/nestjs-socket-handlers-with-ack

Socket.io does not guarantee that the second index in the request payload is the actuall ack.

Closed this issue · 2 comments

The Problem

According to the socket.io documentation, it is not guaranteed that the second index of the request data is the ack.

For example you can emit a request like this:

socket.emit("hello", { a: "b" }, "c", 45, console.log, 678, "Helloooo"); 

In this example the request data array will lock like this:

[{ a: "b" }, "c", 45, [function], 678, "Helloooo"]

When you just blindly access the second index (here) you will end up invoking a string (here) since the "callback" property is the string "c"

According to the socket.io docs the last parameter may be a ack.

Relevant exerp from the socket.io docs.
Sending and getting data (acknowledgements)
Sometimes, you might want to get a callback when the client confirmed the message reception.

>> To do this, simply pass a function as the last parameter of .send or .emit <<

If you want me to send a pull request to fix this, let me know :)

How to fix ?

Just check if the last parameter of the payload is a function.

fixed in 787db33

Fixed in version 1.1.0