Websocket communication with multiple Chrome Docker containers Websocket communication with multiple Chrome Docker containers google-chrome google-chrome

Websocket communication with multiple Chrome Docker containers


You’ve got the basic design correct, but the issue you’re experiencing is with session “stickiness”. However, instead of trying to re-route subsequent requests back to the appropriate machine, we should look for a way to avoid the "pre" request.

The best way to do that is to have your Chrome docker image man-in-the-middle all http “upgrade” requests. This http action is what all WebSocket connections emit prior to changing protocols including the puppeteer library (which is just a WebSocket client under-the-hood). Doing this will also obviate the need for a pre-connect call since the proxying to Chrome will happen on upgrade vs exposing a URL for the app to use. Here's a pretty basic example of doing this with the http-proxy module:

const http = require('http');const httpProxy = require('http-proxy');const proxy = new httpProxy.createProxyServer();http  .createServer()  .on('upgrade', async(req, socket, head) => {      const browser = await puppeteer.launch();      const target = browser.wsEndpoint();      proxyy.ws(req, socket, head, { target })  })  .listen(3000);

There's other benefits with this approach as will: you can limit things like concurrency and even inject scripts to be ran at a later time. Those require a little more though and preparation, but the overall idea remains the same. This also makes load-balancing trivial since there's not need to make routing sticky.

If this is something you're interested in implementing all that works is largely done for you in the browserless repo. It even allows for things like concurrency limitations, session time limitations, and includes a feature-rich IDE. You can find more docs on that project here.