rikulo / socket.io-client-dart

socket.io-client-dart: Dartlang port of socket.io-client https://github.com/socketio/socket.io-client
https://quire.io
MIT License
675 stars 185 forks source link

Library is not compatible with latest version on socket.io 4.7.4 #360

Closed Thioby closed 9 months ago

Thioby commented 9 months ago

Hi, library is no longer compatible with latest socket.io. (4.7.4) In latest version of socket.io there is list reserved events : https://github.com/socketio/socket.io-parser/blob/9f76ea22b41feeade0c8819dbf3c16fd07becc2a/lib/index.ts#L11

/**
 * These strings must not be used as event names, as they have a special meaning.
 */
const RESERVED_EVENTS = [
  "connect", // used on the client side
  "connect_error", // used on the client side
  "disconnect", // used on both sides
  "disconnecting", // used on the server side
  "newListener", // used by the Node.js EventEmitter
  "removeListener", // used by the Node.js EventEmitter
];

In this library list of events looks like this:

const List events = [
  'connect',
  'connect_error',
  'connect_timeout',
  'connecting',
  'disconnect',
  'error',
  'reconnect',
  'reconnect_attempt',
  'reconnect_failed',
  'reconnect_error',
  'reconnecting',
  'ping',
  'pong'
];

as you can see we are missing disconnecting event. Now flutter apps send disconnecting event as EVENT type: 2, but correct type is 1.

As a result, server side socket.io crash:

 Error: invalid payload
    at Decoder.decodeString (/home/app/node_modules/socket.io-parser/build/cjs/index.js:238:23)
    at Decoder.add (/home/app/node_modules/socket.io-parser/build/cjs/index.js:141:27)
    at Client.ondata (/home/app/node_modules/socket.io/dist/client.js:182:26)
    at Socket.emit (node:events:517:28)
    at Socket.onPacket (/home/app/node_modules/engine.io/build/socket.js:120:22)
    at WebSocket.emit (node:events:517:28)
    at WebSocket.onPacket (/home/app/node_modules/engine.io/build/transport.js:94:14)
    at WebSocket.onData (/home/app/node_modules/engine.io/build/transport.js:103:14)
    at WebSocket.<anonymous> (/home/app/node_modules/engine.io/build/transports/websocket.js:20:19)
    at WebSocket.emit (node:events:517:28)
    at Receiver.receiverOnMessage (/home/app/node_modules/engine.io/node_modules/ws/lib/websocket.js:1178:20)
    at Receiver.emit (node:events:517:28)
    at Receiver.dataMessage (/home/app/node_modules/engine.io/node_modules/ws/lib/receiver.js:528:14)
    at Receiver.getData (/home/app/node_modules/engine.io/node_modules/ws/lib/receiver.js:446:17)
    at Receiver.startLoop (/home/app/node_modules/engine.io/node_modules/ws/lib/receiver.js:148:22)
    at Receiver._write (/home/app/node_modules/engine.io/node_modules/ws/lib/receiver.js:83:10)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Receiver.Writable.write (node:internal/streams/writable:337:10)
    at Socket.socketOnData (/home/app/node_modules/engine.io/node_modules/ws/lib/websocket.js:1272:35)
    at Socket.emit (node:events:517:28)
    at addChunk (node:internal/streams/readable:335:12)
    at readableAddChunk (node:internal/streams/readable:308:9)
    at Socket.Readable.push (node:internal/streams/readable:245:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

Error: invalid payload, type (2): \"[\"disconnecting\",\"transport close\"]\n

Apidcloud commented 8 months ago

Just to add some more info to whoever is searching for this issue. I think this happens at least since socket.io 4.6.2 (when they started using socket.io-parser 4.2.4 with the RESERVED_EVENTS listed above).

And just to clarify: should we upgrade to socket io client-dart 3.x beta? or is it something we could avoid by downgrading socket.io to 4.6.1 (and/or force socket.io-parser 4.2.3)?