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.

Cookie based sticky sessions support?

See original GitHub issue

I’m using HAProxy to load balance between different node instances. I’ve enabled sticky sessions using cookies, so that I can easily test the load balancing behind the same remote ip address. But I’m noticing that it doesn’t work in Node.js, while it does work in a browser. The error I receive is xhr poll error, which is what I usually get when I forget to enable sticky sessions. My theory is that socket.io-client running in Node.js, doesn’t accept the cookies from HAProxy. I’ve seen several issues around cookies but never in combination with sticky sessions.

Issues like the following don’t seem hopeful: https://github.com/Automattic/engine.io-client/issues/304 https://github.com/Automattic/socket.io-client/pull/587

Code example:

var io = require('socket.io-client');
var socket = io("https://myserver.com");
socket.on("connect",function() {
  console.log("connected");
});
socket.on("error",function(data) {
  console.log("error: ",data);
});

HAProxy:

global

    # By default, only one process is created, which is the recommended mode of operation. 
    nbproc                      1
    maxconn                     4096

    # Logging
    log 127.0.0.1 local0

    ## SSL
    # Set DH params to 2048 bits
    tune.ssl.default-dh-param 2048
    # Strong ciphers
    # Do not forget to add 'no-sslv3' to each ssl enabled frontend (POODLE)
    ssl-default-bind-ciphers        EECDH+AES:EDH+AES:-SHA1:EECDH+AES256:EDH+AES256:AES256-SHA:!aNULL:!eNULL:!EXP:!LOW:!MD5:!RC4
    ssl-default-server-ciphers      EECDH+AES:EDH+AES:-SHA1:EECDH+AES256:EDH+AES256:AES256-SHA:!aNULL:!eNULL:!EXP:!LOW:!MD5:!RC4

defaults
    mode http
    log global
    # this enables logging of HTTP request, session state and timers
    option httplog
    # enable HTTP connection closing on the server side
    option http-server-close
    # disable logging of null connections as these can pollute the logs
    option dontlognull
    # enable session redistribution in case of connection failure, which is important in a HA environment
    option redispatch
    option contstats
    # enables the insertion of the X-Forwarded-For header to requests sent to servers
    option forwardfor
    # Default timeouts
    # See also http://blog.haproxy.com/2012/11/07/websockets-load-balancing-with-haproxy/
    timeout connect 5s
    timeout client 30s
    timeout server 30s
    timeout tunnel 3600s
    timeout http-keep-alive 1s
    timeout http-request 15s
    timeout queue 30s
    timeout tarpit 60s
    timeout check 5s
    retries 3
    backlog 4096
    default-server inter 3s rise 2 fall 3

frontend https
    bind 0.0.0.0:443 ssl crt /etc/haproxy/ssl/server.pem no-sslv3 
    option              httpclose
    default_backend     nodeapp
    tcp-request inspect-delay 500ms
    tcp-request content accept if HTTP

backend nodeapp
    mode                http
    balance             leastconn
    option              forwardfor
    option              http-server-close
    option              forceclose
    no option           httpclose

    http-request set-header X-Forwarded-Port %[dst_port]
    http-request add-header X-Forwarded-Proto https if { ssl_fc }

    cookie SRV_ID insert

    server nodeapp1 127.0.0.1:5000 cookie nodeapp1 check
    server nodeapp2 127.0.0.1:5001 cookie nodeapp2 check
    server nodeapp3 127.0.0.1:5002 cookie nodeapp3 check
    server nodeapp4 127.0.0.1:5003 cookie nodeapp4 check

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jonathanrozecommented, Sep 17, 2022

@jonathanroze hi! Please check the example here: https://socket.io/how-to/deal-with-cookies#nodejs-client-and-cookies

Thanks a lot !!

1reaction
darrachequesnecommented, Sep 17, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sticky sessions for your Application Load Balancer
Application Load Balancers support both duration-based cookies and application-based cookies. Sticky sessions are enabled at the target group level.
Read more >
What is Session Stickiness | Pros and Cons of ... - Imperva
With sticky sessions, a load balancer assigns an identifying attribute to a user, typically by issuing a cookie or by tracking their IP...
Read more >
Cookies, Sessions, and Persistence - F5 Networks
Persistence—otherwise known as stickiness—is a technique implemented by ADCs to ensure requests from a single user are always distributed to the server on...
Read more >
Sticky and NON-Sticky sessions - Stack Overflow
When your website is served by only one web server, for each client-server pair, a session object is created and remains in the...
Read more >
Sticky session with cookies: not as dirty as it sounds - Resources
Some kind of cookie monster · when we receive a request (in vcl_recv), we set a cookie if none was present, then assign...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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