Our project is composed of a set of nodejs micro services, communicating through socket.io. Some of those micro services are running behind an amazon ALB load balancer. The only available session stickiness available to ALB target groups is cookie-based and the connections are allocated using a round-robin algorithm when the session cookie is not specified in the http request.
This effectively breaks the handshake protocol occurring when the socket.io connection is established, leading to the dreaded xhr poll error 400 response.
A workaround consists in disabling the xhr polling altogether in the client and only rely on the actual websocket. This is perfect in a controlled environment, but does not work when some of the services are developed and hosted by third parties.
Another hack also exists, which consists in overriding the base require('http').request function call, and issuing a pre-flight request in order to fetch those session cookies. Further http requests would then reinsert the cookies in the request. At best, I call that a hack.
See ALB Sticky Sessions
Another request for the same feature #775
Any updates on this?
anyone ever solve this?
@AndyGOBrien one workaround is to get your cookies from the server first and then open the socket connection specifying the extraHeaders.Cookie
option as this:
const socket = io(url, {
extraHeaders: {
Cookie: cookiesString
}
})
Docs: https://github.com/socketio/engine.io-client#nodejs-with-extraheaders
@turpault there's an io
cookie set during the handshake: https://github.com/socketio/engine.io#methods-1
Yes, i got this to work. Thanks guys
@AndyGOBrien were you doing this in browser or in node? i'm able to get it working in node, but in browser i'm unable to set the cookie
header
socket.io-client-v2.1.1 currently extraHeaders.Cookie
option works only on NodeJS
at Webpack 3.11.0 / Mac 10.13.6 / Google Chrome 67.0.3396.87
@AndyGOBrien one workaround is to get your cookies from the server first and then open the socket connection specifying the
extraHeaders.Cookie
option as this:const socket = io(url, { extraHeaders: { Cookie: cookiesString } })
Docs: https://github.com/socketio/engine.io-client#nodejs-with-extraheaders
Any working example? Thanks
Warning: The solution above does not handle AWSALB timeout. If the socket tries to reconnect after stickiness-timeout (i.e. 24hrs), it will throw transport error
.
socket.io-client-v2.1.1 currently
extraHeaders.Cookie
option works only on NodeJS
at Webpack 3.11.0 / Mac 10.13.6 / Google Chrome 67.0.3396.87
I got same problem. any luck it got fixed already?
@Seldonm @gabmontes @59naga any luck that anyone ever get a successful case with extraHeaders?
I am not able to do it at all.
Any update on this? There are load balancer that do work only with Cookie sticky session, like Traefik...
Most helpful comment
@AndyGOBrien one workaround is to get your cookies from the server first and then open the socket connection specifying the
extraHeaders.Cookie
option as this:Docs: https://github.com/socketio/engine.io-client#nodejs-with-extraheaders