question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Session ID unknown" after handshake on high server load [Socket.io 1.0.6]

See original GitHub issue

I am running a multi-node server (16 workers running Socket.io 1.0.6; accessed via Nginx, configured as a reverse proxy supporting sticky sessions) for ~ 5k users. While the load of the server is low (2~3 on a 20 core server / 2k users), everyone is able to connect instantly. When the load of the server gets higher (5~6 / 5k users), new users are not able to connect and receive data instantly. In this case, it takes 2~4 handshakes for the users to connect succesfully.

This is what happens (high load):

  • User opens the website; receives HTML and JS
  • User’s browser attempts to initialize a socket.io connection to the server (io.connect(...))
  • A handshake request is sent to the server, the server responds with a SID and other information ({"sid":"f-re6ABU3Si4pmyWADCx","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000})
  • The client initiates a polling-request, including this SID: GET .../socket.io/?EIO=2&transport=polling&t=1408648886249-1&sid=f-re6ABU3Si4pmyWADCx
  • Instead of sending data, the server responds with 400 Bad Request: {"code":1,"message":"Session ID unknown"}
  • The client performs a new handshake (GET .../socket.io/?EIO=2&transport=polling&t=1408648888050-3, notice the previously received SID is omitted)
  • The server responds with new connection data, including a new SID: ({"sid":"DdRxn2gv6vrtZOBiAEAS","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000})
  • The client performs a new polling request, including the new SID: GET .../socket.io/?EIO=2&transport=polling&t=1408648888097-4&sid=DdRxn2gv6vrtZOBiAEAS
  • The server responds with the data that is emitted in the worker source code.

Depending on the load of the server, it may happen 1~3 times that the server responds with "Session ID unknown" and the client needs to perform a new handshake before data is actually received.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:7
  • Comments:27 (6 by maintainers)

github_iconTop GitHub Comments

38reactions
p3x-robotcommented, Nov 11, 2017

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,
    });
10reactions
jaykumarthakercommented, Jan 15, 2020

DON’T FORGET TO CONFIGURE CLIENT AS WELL

Making just nodejs backend to use transport as websocket protocol won’t do much. socket.io clients are also required to set with the same configuration. So, in my onion below should work:

in nodejs:

  const ioSocket = io('',  {
      transports: ['websocket',  'polling']
    });

and in js client:

  socket = io.connect(SocketServerRootURL, {
        transports:['websocket', 'polling']
    });

['websocket', 'polling'] will force socket.io to try webscoket as the first protocol to connect, otherwise fall back to polling (just in case some browsers/clients may not support websockets). For cluster environment, better to use ['websocket'] only.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js with Socket.io - Long Polling fails and throws "code":1 ...
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...
Read more >
"Session ID unknown" after handshake on high server load [Socket ...
I am running a multi-node server (16 workers running Socket.io 1.0.6; accessed via Nginx, configured as a reverse proxy supporting sticky sessions) for...
Read more >
Using multiple nodes | Socket.IO
IO servers, there are two things to take care of: ... experience HTTP 400 errors due to "Session ID unknown"; the WebSocket transport...
Read more >
Module ngx_http_ssl_module - Nginx.org
Since version 1.11.0, this directive can be specified multiple times to load certificates of different types, for example, RSA and ECDSA: server {...
Read more >
primus - npm
In the above example we automatically create a HTTP server which will listen on port 8080, a primus instance with the websockets transformer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found