title: Expo React Native application inside of iOS Simulator keeps reconnecting using socket.io
body: <p>The stack is atm <code>VueJS</code>, <code>React Native</code> using <code>Expo</code>, and <code>NestJS</code> for the backend. For convenience, <a href="https://github.com/felipecrescencio/nest-chat-websockets" rel="nofollow noreferrer">this repo</a> contains a working example of VueJS and NestJS parts. The connection from the React Native side (simplified version) is implemented like this:</p>
  <pre><code>export const useSubscribeToWebsocket = (token: string) =&gt; {
    const { socketUrl } = usePlatform()
  
    useEffect(() =&gt; {
      console.log('open')
      const socket = io(socketUrl)
  
      socket.on('connect', () =&gt; {
        console.log('connect')
        socket.send(JSON.stringify(authenticate(token)))
      })
  
      socket.on('error', (error: any) =&gt; {
        dlog('error connection=&gt; ', error)
      })
  
      socket.on('event', (socketEvent: any) =&gt; {
        console.log('event ', socketEvent)
      })
  
      return () =&gt; {
        socket.close()
      }
    }, [token])
  
    return null
  }
  </code></pre>
  <p>The gateway (socket server) looks pretty straightforward:</p>
  <pre><code>@WebSocketGateway()
  export class AppGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
    @WebSocketServer() server: Server;
    private logger: Logger = new Logger('AppGateway');
  
    @SubscribeMessage('msgToServer')
    handleMessage(client: Socket, payload: string): void {
      this.server.emit('msgToClient', payload);
    }
  
    afterInit(server: Server) {
      this.logger.log('Init');
    }
  
    handleDisconnect(client: Socket) {
      this.logger.log(`Client disconnected: ${client.id}`);
    }
  
    handleConnection(client: Socket, ...args: any[]) {
      this.logger.log(`Client connected: ${client.id}`);
    }
  }
  </code></pre>
  <p>The problem is that while the mobile client connects only once (the <code>useEffect</code> hook is called one single time, no other logs except <code>open</code>), <code>handleConnection</code> keeps logging clients with different ids being connected. <code>connect</code> is also never logged on the RN side, so I suppose it never connects.
  I tested it with VueJS client, and it connects only once (works fine on backend as well), everything works.</p>
  <p>Is it a problem related to the simulator only? Is there a way to fix it?</p>