I'm using Node.js to spawn puppeteer in a separate process.
Here's that child process (it writes the websocket to the standard output stream):
// path: cp/index.js
(async () => {
const browser = await puppeteer.launch();
const wsEndpoint = browser.wsEndpoint();
process.stdout.write(wsEndpoint);
})();
Within the main process, I'm connecting to the puppeteer instance using the websocket. Like so:
var cp = await spawn('node', ['./cp/index.js'], {
detached: true,
shell: true,
cwd: __dirname,
});
cp.stdout.on('data', async (ws) => {
let browser = await puppeteer.connect({ browserWSEndpoint: ws.toString() });
// Do some processing.
// Exit the child process.
});
But unfortunately, I'm unable to access the child process within the cp.on('data')
callback, in order to exit after processing.
How can I get access to the websocket from my child process, connect in the main process, perform some asynchronous code, and then disconnect from the child process?