Node.js with Socket.io - Long Polling fails and throws “code”:1,“message”:“Session ID unknown” response

血红的双手。 提交于 2019-12-01 03:35:01

For socket.io v2.0.3 (js client),when you use socket.io on client side the client socket.io makes some network calls as explained:

  1. When io.connect() is called, the socket.io library makes a call to the server which looks like

    ?EIO=3&transport=polling&t=LqtOnHh,

    server responds with something like

    "90:0{"sid":"pcJM_AEZirrJT-DuAAUy","upgrades[], "pingInterval":3600000,"pingTimeout":3600000}2:40"

    here the server generates a socket object on server side and sends its id back to the client.

  2. After this client makes another call to the server which is something like

    ?EIO=3&transport=polling&t=LqtR6Rn&sid=0JFGcEFNdrS-XBZeAAXM

    this is the long poll call that client makes to the server, if you see here it is passing the sessionId which it received in first call above, if the call goes to same node which generated that sessionId, the node identifies the socket connection for which the request has been made and responds.

    But behind ELB the call may go to some other node that didn't generate this sessioId, in that case the node will not be able to identify the sessionId for which the call was made and hence responds with {"code":1,"message":"Session ID unknown"}

You will also see this error in case of long polling not getting answered or getting timeout.

In my project, I tried adding the transport option as a websocket and error no longer appears.

io.connect(url, {
   "transports": ['websocket']
})

It will be a problem with supporting IE and other old browsers as they don't support native websockets, if the application browser compatibility is not an issue this seems a solution.

antono

If you use Socket.io-Redis then it looks like a situation described in bug tracker of Socket.io:

https://github.com/socketio/socket.io/issues/1739#issuecomment-64244359

for me, it was with nginx ssl http2, and it was polling, so the good config is:

 const ioSocket = io('', {
      // Send auth token on connection, you will need to DI the Auth service above
      // 'query': 'token=' + Auth.getToken()
      path: '/socket.io',
      transports: ['websocket'],
      secure: true,
    });

Had the same error recently with socket.io: {"code":1,"message":"Session ID unknown"} for web sockets connection, though setup is different (nginx + passenger + nodejs). The issue was broken connection from NodeJS to Redis which I have in my NodeJS app. So once Redis was properly started (it was an IP binding issue), socket.io started to communicate properly via web sockets. If you're stuck on the same problem, check that your NodeJs app is working properly (e.g. it connects to all required resources)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!