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

React native ios crashes on production with extraHeaders

Open ouarrtaha opened this issue 2 years ago • 4 comments

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:

  • Device: iphone 11
  • OS: ios 17

ouarrtaha avatar Oct 24 '23 19:10 ouarrtaha

Update:

it worked with

transportOptions: {
    polling: {
      extraHeaders: {
        'Cookie': cookie,
      },
    },
  }

ouarrtaha avatar Oct 25 '23 00:10 ouarrtaha

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?

darrachequesne avatar Oct 25 '23 13:10 darrachequesne

i'm using react native 0.72.6

ouarrtaha avatar Oct 25 '23 23:10 ouarrtaha

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

darrachequesne avatar Apr 08 '24 16:04 darrachequesne