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

socket.onAnyOutgoing() cannot receive binary when it uses room

Open zoeponta opened this issue 3 years ago • 2 comments

Describe the bug

I want to use socket.onAnyOutgoing() to calculate bytes of outgoing messages. However, when I use a room and send a binary message, socket.onAnyOutgoing() cannot receive it.
So, I cannot calculate correctly. I can reproduce it with the following code.

To Reproduce

Socket.IO server version: 4.5.0

Server

io.on('connection', (socket: Socket) => {
  socket.onAnyOutgoing((event: string, ...args: any[]) => {
    console.log('onAnyOutgoing:', ...args)
  })

  socket.join('test')
  socket.emit('message', 'hoge')                     // -> onAnyOutgoing: hoge
  socket.emit('message', Buffer.from('hoge'))        // -> onAnyOutgoing: <Buffer 68 6f 67 65>
  io.to('test').emit('message', 'hoge')              // -> onAnyOutgoing: hoge
  io.to('test').emit('message', Buffer.from('hoge')) // -> onAnyOutgoing: { _placeholder: true, num: 0 }
})

Expected behavior

socket.onAnyOutgoing() should receive correctly binary message when it uses room.

  io.to('test').emit('message', Buffer.from('hoge')) // -> onAnyOutgoing:  <Buffer 68 6f 67 65>

Platform:

  • OS : Generic

zoeponta avatar May 17 '22 14:05 zoeponta

Hi! I could indeed reproduce the issue.

It's because the object is edited in place when encoding: https://github.com/socketio/socket.io-adapter/blob/b92d65cf9c6124930fac78349e2a0b847fe19f16/lib/index.ts#L148

Not sure if we can fix this without losing some performance on the table.

darrachequesne avatar May 23 '22 06:05 darrachequesne

Thanks for the reply!

This problem seems to be difficult to fix. So, I will change my code to convert message from binary to string.

zoeponta avatar May 27 '22 12:05 zoeponta

Update: this should be fixed in latest release, see https://github.com/socketio/socket.io-parser/commit/ae8dd88995dbd7f89c97e5cc15e5b489fa0efece.

Please reopen if needed.

darrachequesne avatar Jan 21 '23 11:01 darrachequesne