how to get WSendpoint of a puppeteer-cluster browser

703 views Asked by At

in a project that requires:

  • Starting each session with logging in credentials + notification/OTP
  • work with multiple accounts asynchronously
  • remote debugging or monitoring of the session
  • 5+ different operations can be requested on a open session , in any order

I want it to be a puppeteer cluster browser with one remote debugging port to monitor it but couldn't integrate WsEndpoints

const puppeteer = require('puppeteer-extra');

const { Cluster } = require('puppeteer-cluster');

class SingleBrowserCluster {

  browserInstance
  options

  constructor() {
   
      if (SingleBrowserCluster._instance) {
          
        //throw new Error("Singleton classes can't be instantiated more than once.")
      }
else{
      SingleBrowserCluster._instance = this;
      // ... Your rest of the constructor code goes after this

      console.log("pre optArgs");
      const optArgs = [
          '--remote-debugging-port=3002',//works if dockerised 
          '--remote-debugging-address=0.0.0.0',// at localhost.3002
          '--window-size=1920,1080',
          '--no-sandbox',
          '--disable-setuid-sandbox',
          '--disable-gpu', '--no-zygote',    //'--single-process',     
      ];

      console.log("pre options");
      this.options = {
          headless: true,//for dockerization
          args: optArgs,
          defaultViewport: null,
          waitUntil: 'networkidle2'
      };

      console.log("Do launch now");


      return this; 
    }
  }
  
  
  async screenshotMethod({ page, data: url }) {
      
      await page.goto(url);
      
      console.log(`%c worker X is running on ${url} `, `color:green;`);
      console.log("will wait 20 second");
      await page.waitForTimeout(20000)
      
      const path = url.replace(/[^a-zA-Z]/g, '_') + '.png';
      await page.screenshot({ path });
  };
  
  
      
  async launchCluster (){
    try {
      
            this.browserInstance =  await Cluster.launch({
                concurrency: Cluster.CONCURRENCY_CONTEXT,
          maxConcurrency: 3,
          puppeteerOptions: this.options
      })

      console.log(this.browserInstance);
      return this.browserInstance;
  
  } catch (error) {
      console.log(`%c ERRORR`,`color:red;`);
      console.log(error);
  }
  }
  
}


const StartScraper = async (Url, useProxy) => new Promise((resolve, reject) => {
  (async () => {
    // get proxy url from environment files
    const proxyUrl = process.env.PROXY_URL;

//--disable-dev-shm-usage
    // By default, Docker runs a container with a /dev/shm shared memory space 64MB. This is typically too small for Chrome and will cause Chrome to crash when rendering large pages. 
    //his will write shared memory files into /tmp instead of /dev/shm. See crbug.com/736452 for more details.



    var instanceOne1= new SingleBrowserCluster()//.launchCluster()

    var browser= await instanceOne1.launchCluster();
    browser.queue('https://www.google.com/', instanceOne1.screenshotMethod);

//THE PROBLEM LINE
    const wsEndpoint = browser.wsEndpoint();



    try {
      const page = (await browser.pages())[0];
      await page.goto(Url, { waitUntil: 'load' });
      return resolve(wsEndpoint);
    } catch (e) {
      browser.close();
      return reject(false);
    }
  })();
});

how can i have WSendpoint of any session in a puppeteer-cluster

( more info: i will put those in a session file to provide my next selected consequtive operation a connection point on its session

  • localhost/StartScraper creates WSendpoint
  • localhost/login==WSendpoint==>Connects to existing session do login stuff
  • localhost/listItems==WSendpoint==>Connects to existing session do listItems stuff ... )
0

There are 0 answers