socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Add response Ack in socket.prependAnyOutgoing()

Open NguyenVanCong1902 opened this issue 2 years ago • 3 comments

I'm using

  socket.use(([event, ...args], next) => {
    incommingEventFilter(socket, [event, args], next);
  });
  socket.prependAnyOutgoing(outgoingEventFilter);

to filter and log all income & outgoing event;

It work well...but...

Client-Side:

socket.emitWithAck("hello", "world", (err, val) => {
  //...
});

Server-Side:

socket.on("hello", (arg, callback) => {
    console.log(arg); // "world"
    callback("got it");
});

In postman client, i received event 'hello' with data 'got it';

The one thing is event 'hello' with 'got it' was not logged by socket.prependAnyOutgoing()

Describe the solution you'd like Please add callback Ack to socket.prependAnyOutgoing() or is there any way to log all callback Ack 🤔🤔

NguyenVanCong1902 avatar Mar 03 '23 09:03 NguyenVanCong1902

Do you need help with this? If yes you can definitely assigned this to me. From what I can understand the crux of the issue is that the socket.prependAnyOutgoing method in socket.io does not intercept acknowledgments/callbacks. It is designed to intercept outgoing events only, not the corresponding acknowledgments/callbacks. If you want to log all callback acknowledgments, you need to modify your code by intercepting the socket.emitWithAck method instead

Shekharcodess avatar May 29 '23 17:05 Shekharcodess

Do you need help with this? If yes you can definitely assigned this to me. From what I can understand the crux of the issue is that the socket.prependAnyOutgoing method in socket.io does not intercept acknowledgments/callbacks. It is designed to intercept outgoing events only, not the corresponding acknowledgments/callbacks. If you want to log all callback acknowledgments, you need to modify your code by intercepting the socket.emitWithAck method instead

        if (typeof callback === 'function') {
            //override Ack Callback to log event callback
            let tempFunc = callback;
            callback = (response) => {
                tempFunc(response);
                logger.info(`OUTGOING_EVENT(Callback) ==> ${socket.id} ${JSON.stringify(socket.data.userData) || ''}: message with ${response}`);
            }
        }

I'm currently using this to override callback function inside every socket.on() event. It's not an intercepting but it's still good 😂😂. I assigned this issue for you, hopfully we will have a better solution. Thank you in advance 😘😘😘

NguyenVanCong1902 avatar May 30 '23 02:05 NguyenVanCong1902

Ops, sorry, seem i don't have assign permission....

NguyenVanCong1902 avatar May 30 '23 02:05 NguyenVanCong1902