Get
The following example demonstrates how to request a browser instance, navigate to a webpage, disconnect the browser, retrieve details about all active browsers, and close them.
const svc = new BrowserService(cloudBrowserToken);
const rp = await svc.open();
if (rp.status !== ResponseStatus.SUCCESS || rp.address == null) {
console.log(`Error requesting browser: ${rp.status}`);
return;
}
console.log("Browser requested");
const browser = await puppeteer.connect({
browserWSEndpoint: rp.address,
defaultViewport: null,
slowMo: 0,
});
console.log("Browser connected");
const page = (await browser.pages())[0];
await page.goto("http://www.cloudbrowser.ai");
console.log("Web visited");
browser.disconnect();
console.log("Browser disconnected");
const rpGet = await svc.get();
console.log("Label | Address | Started On | VNC Opened | VNC Pass");
for (const b of rpGet.browsers) {
console.log(
`${b.label} | ${b.address} | ${b.startedOn} | ${b.vncPass !== null} | ${b.vncPass}`
);
await svc.close(b.address);
}
Full example
The svc.get()
method is crucial for managing and monitoring active browser sessions. Here's why it's important:
Session Retrieval: It provides a list of all currently active browser sessions, enabling visibility into ongoing operations.
Detailed Insights: Each session includes metadata like:
Label: Identifies the session for organization.
Address: The WebSocket endpoint to reconnect if needed.
Started On: Tracks when the session was initiated.
VNC Status and Password: Indicates whether a remote desktop (VNC) is active and provides access credentials.
Session Cleanup: By iterating through the retrieved sessions, you can close inactive or unneeded sessions, freeing up resources and preventing unnecessary costs.
In this script, svc.get()
ensures that no orphaned browser sessions remain by listing all active sessions and systematically closing them.
Last updated