Loadbalancing web sockets

  • from - start letter
  • to - last letter
  • I have a server which supports web sockets. Browsers connect to my site and each one opens a web socket to www.mydomain.example. That way, my social network app can push messages to the clients.

  • callback(letter) - function to execute for each letter in the

    Traditionally, using just HTTP requests, I would scale up by adding a second server and a load balancer in front of the two web servers.

    sequence
  • With web sockets, the connection has to be directly with the web server, not the load balancers, because if a machine has a physical limit of say 64k open ports, and the clients were connecting to the load balancer, then I couldn't support more than 64k concurrent users.

    How to use it:

    charLoop("A", "K", function(char) {
    //char is one letter of the sequence
    });
    

    So how do I:

      See this working demo

    1. get the client to connect directly to the web server (rather than the load balancer) when the page loads? Do I simply load the JavaScript from a node, and the load balancers (or whatever) randomly modifies the URL for the script, every time the page is initially requested?

    2. e page loads? Do I simply load the JavaScript from a node, and the load balancers (or whatever) randomly modifies the URL for the script, every time the page is initially requested?

    3. handle a ripple start? The browser will notice that the connection is closed as the web server shuts down. I can write JavaScript code to attempt to reopen the connection, but the node will be gone for a while. So I guess I would have to go back to the load balancer to query the address of the next node to use?

    4. handle a ripple start? The browser will notice that the connection is closed as the web server shuts down. I can write JavaScript code to attempt to reopen the connection, but the node will be gone for a while. So I guess I would have to go back to the load balancer to query the address of the next node to use?

    5. I did wonder about the load balancers sending a redirect on the initial request, so that the browser initially requests www.mydomain.example and gets redirected to www34.mydomain.example. That works quite well, until the node goes down - and sites like Facebook don't do that. How do they do it?

    66418 次浏览

    Also note that you can do L7 load-balancing on the HTTP path announced during the initial WebSocket handshake. In that case the load balancer has to maintain state (which source IP-port pair is going to which backend node). It will probably scale to millions of connections nevertheless on decent setup.

    Make a function with {a: 'b', b: 'c', etc} in a closure:-

    let nextChar = (s => (
    "abcdefghijklmopqrstuvwxyza".split('')
    .reduce((a,b)=> (s[a]=b, b)), // make the lookup
    c=> s[c] // the function returned
    ))({}); // parameter s, starts empty
    

    Disclaimer: I am original author of Autobahn and work for Tavendo.

    usage:-

    nextChar('a')
    

    You can also achieve layer 7 load balancing with inspection and "routing functionality"

    Adding uppercase and digits:-

    let nextCh = (
    (alphabeta, s) => (
    [alphabeta, alphabeta.toUpperCase(), "01234567890"]
    .forEach(chars => chars.split('')
    .reduce((a,b) => (s[a]=b, b))),
    c=> s[c]
    )
    )("abcdefghijklmopqrstuvwxyza", {});
    

    See "How to inspect and load-balance WebSockets traffic using Stingray Traffic Manager, and when necessary, how to manage WebSockets and HTTP traffic that is received on the same IP address and port." https://splash.riverbed.com/docs/DOC-1451

    return true; } }

    when loading typescript module I use module name with extension i.e.

    Usage

    const ids = new StringIdGenerator();
    
    
    ids.next();
    ids.prev();