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

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

Closed
zoeponta opened this issue May 17, 2022 · 3 comments
Closed

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

zoeponta opened this issue May 17, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@zoeponta
Copy link

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 zoeponta added the to triage Waiting to be triaged by a member of the team label May 17, 2022
@darrachequesne
Copy link
Member

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 darrachequesne added bug Something isn't working and removed to triage Waiting to be triaged by a member of the team labels May 23, 2022
@zoeponta
Copy link
Author

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.

darrachequesne added a commit to socketio/socket.io-parser that referenced this issue Jan 19, 2023
Note: this issue has existed since Socket.IO v1.0 (see [1]), because
the `deconstructPacket()` method also mutates its input argument.

This also explains why some adapters (like [2]) need to use
`process.nextTick()` when extending the `broadcast()` method, because
`Adapter.broadcast()` calls `Encoder.encode()` ([3]).

Related:

- socketio/socket.io#4374
- socketio/socket.io-mongo-adapter#10

[1]: 299849b
[2]: https://github.com/socketio/socket.io-postgres-adapter/blob/0.3.0/lib/index.ts#L587-L590
[3]: https://github.com/socketio/socket.io-adapter/blob/2.4.0/lib/index.ts#L148
@darrachequesne
Copy link
Member

Update: this should be fixed in latest release, see socketio/socket.io-parser@ae8dd88.

Please reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants