socketio / socket.io

Realtime application framework (Node.JS server)
https://socket.io
MIT License
61.2k stars 10.11k forks source link

React native ios crashes on production with extraHeaders #5120

Open ouarrtaha opened 1 year ago

ouarrtaha commented 1 year ago

I need to pass a cookie, for the ingress session affinity, in order to maintain connection in multi pods, so i set the extraHeaders:

when running the app in debug (in simulator or device) it works as expected, but after releasing ios app in testflight and testing, it crashes,

 Fatal Exception: NSInvalidArgumentException
-[__NSArrayM length]: unrecognized selector sent to instance 0x283dcfde0

To Reproduce

Socket.IO server version: 4.4.0

Server

const io = new Socket(server, {
    transports: ['websocket', 'polling'],
    cors: {
      origin: '*',
      methods: ['GET', 'POST'],
      credentials: true,
    },
    adapter: redisAdapter({
      pubClient: redisClient,
      subClient: redisClient.duplicate(),
    }),
  });

Socket.IO client version: 4.7.2

Client

initSocket(userId, cookie) {
    this.socket = io(SOCKET_URL, {
      query: `userId=${userId}`,
      autoConnect: false,
      transport: ['polling', 'websocket'],
      extraHeaders: {
        'Cookie': cookie,
      },
    });
  }

Expected behavior Connect without crash and maintain connection

Platform:

ouarrtaha commented 1 year ago

Update:

it worked with

transportOptions: {
    polling: {
      extraHeaders: {
        'Cookie': cookie,
      },
    },
  }
darrachequesne commented 1 year ago

Hi! Thanks for the report :+1:

The only difference between extraHeaders: { ... } and transportOptions: { polling: { extraHeaders }}} is that, in the first case, the extraHeaders is also used when establishing the WebSocket connection.

Source: https://github.com/socketio/engine.io-client/blob/3dcb88cb31a67d19807e70b16203a9a3cf8c129d/lib/transports/websocket.ts#L70-L72

Which React Native should support I think.

Reference: https://reactnative.dev/docs/network#websocket-support

Which version of React Native are you using?

ouarrtaha commented 1 year ago

i'm using react native 0.72.6

darrachequesne commented 7 months ago

@ouarrtaha I was not able to reproduce. Were you able to find the culprit? Is it fixed in newer React native versions?