Node.js with Socket.io - Long Polling fails and throws "code":1,"message":"Session ID unknown" response
Asked Answered
P

6

26

I'm stuck on why a node.js app that was moved to an IIS7 server is now failing. I know IIS7 doesn't support web sockets but my understanding was that socket.io would fall back to long polling if web socket isn't available. So now when the user tries to press a specific button which would normally have required the socket or long polling I get something like this:

XHR finished loading: POST "https://localhost:817/socket.io/?EIO=2&transport=polling&t=1433777964357-6&sid=QWsESi0c9ih7WMWKAAAC".
GET https://localhost:817/socket.io/?EIO=2&transport=polling&t=1433777963494-5&sid=QWsESi0c9ih7WMWKAAAC 400 (Bad Request)
XHR finished loading: GET "https://localhost:817/socket.io/?EIO=2&transport=polling&t=1433777963494-5&sid=QWsESi0c9ih7WMWKAAAC".
OPTIONS https://localhost:817/socket.io/?EIO=2&transport=polling&t=1433777965127-7&sid=QWsESi0c9ih7WMWKAAAC 
XMLHttpRequest cannot load https://localhost:817/socket.io/?EIO=2&transport=polling&t=1433777965127-7&sid=QWsESi0c9ih7WMWKAAAC. Invalid HTTP status code 400

When I click on the GET or the XMLHttpRequest I can see that the response is "code":1,"message":"Session ID unknown", which I don't understand as I can see the SID. When I click on the code listed for the failure for the option I see that the problem is coming from the Request.prototype.create, namely the xhr send:

xhr.send(this.data);

Does anyone have any idea what could be causing these things?

Any clarification would be greatly appreciated!

Thank you so much!

Petrify answered 8/6, 2015 at 15:45 Comment(0)
C
35

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 load balancer the call may go to some other node that didn't generate this sessionId, 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.

Capitalization answered 12/7, 2017 at 16:30 Comment(7)
Thanks a lot for explaining the issue as mine problem is same as you explained but can you please suggest a solution..??Littlefield
you need to maintain stickiness at your load balancerCapitalization
read about stickiness you will get to know, the only workaround is you don't using polling from socket.io library and just use websockets config from itCapitalization
@ShahidHussain I have facing same error, can you please suggest solution to remove this error and run nodejs app on Load Balancer with multiple servers. It will help me if you share solution. Thanks in advance.Maxinemaxiskirt
I have been running a single server. On the first request with sid, returns 200, but after that requests with the same sid server responding 400Nickel
Yeah, my issue was that there were two nodes for one chat, we left only one node for chat and it fixed the issue.Olympia
thanks after enable sticky load balancer. error gone for me.Discontinuity
S
13

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.

Snell answered 14/9, 2016 at 6:43 Comment(2)
I was having this issue in chrome. was struggling to fix this for last 1day. Thanks a lot for your solution.Bridges
@saeta, At first I have done the same, and it worked fine as you said. But after some time like 2-3 months, it started again😐Adenoid
A
9

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,
    });
Aliphatic answered 11/11, 2017 at 12:45 Comment(0)
S
2

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

Slusher answered 26/10, 2015 at 8:48 Comment(0)
C
1

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)

Catatonia answered 21/6, 2016 at 19:27 Comment(0)
S
0

from what I know you need to enable sticky session for long polling to work. you can read about how to do this here in socket io docs

or you can just disable polling in you socket setup as it stated by @saeta in this answer

Scleroma answered 31/5, 2024 at 10:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.