Skip to main content

Watching Sessions

The Browserless Docker image and Dedicated hosted plans support real-time session watching for debugging and monitoring.

This guide covers three methods for watching sessions:

  • Using the Docker image session viewer
  • Using the Sessions tab in Dedicated plans
  • Programmatically watching sessions via GraphQL API

Watching sessions on the Docker image

To use the session viewer on the Docker image, you don't need to do anything special in your code or how you connect to Browserless. Optionally, you can add an id parameter to your connect call to sort or filter sessions to the one you care about.

Start the container as usual:

$ docker run --rm -p 3000:3000 ghcr.io/browserless/chrome

Then, in your application or script connect to it.

import puppeteer from "puppeteer";

const browser = await puppeteer.connect({
browserWSEndpoint: `wss://YOUR_CONTAINER_URL_HERE?token=YOUR_API_TOKEN_HERE`,
});
const page = await browser.newPage();
await page.goto("https://example.com/");
await browser.close();

In your browser, you can go to http://localhost:3000/sessions to get a JSON of the running sessions.

info

If your session is immediately closing, then you can keep it open by not calling browser.close at the end (just remember to terminate your script with CTRL+C or similar).

Using the Sessions tab in Dedicated plans

In Dedicated plans, you can see your active sessions at any time by going to the Sessions portion of the account page.

To view running sessions:

  1. Click the "Fetch Running Sessions" button to retrieve actively running sessions across all your workers
  2. Click the "Debugger" link to open and view specific sessions

Sessions appear in the list immediately after connecting. Refresh by clicking "Fetch Running Sessions".

Programmatically Watching Sessions on Dedicated Plans

The GraphQL API lets you retrieve running sessions programmatically. Common use cases:

  • End-users can watch their automation running in real time
  • Engineers can debug live production issues
  • Teams can pinpoint problems in scripts faster

Security note: When setting this up, ensure you don't accidentally expose non-relevant sessions among your users. Use a unique id parameter to filter and distinguish between running sessions.

Here's a complete example showing how to connect a browser, fetch the session via GraphQL, and get a live debugging URL.

import puppeteer from "puppeteer";

const apiToken = "YOUR_API_TOKEN_HERE";
const id = "some-unique-id";

const getSessionsById = (id) => {
return fetch("https://api.browserless.io/graphql", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
query: `
query getSessions($id: String!, $apiToken: String!) {
sessions(apiToken: $apiToken, id: $id) {
description
devtoolsFrontendUrl
id
title
url
browserId
browserWSEndpoint
}
}
`,
variables: {
id,
apiToken,
},
}),
})
.then((res) => res.json())
.then((res) => res.data.sessions)
.catch((error) => {
console.log(`Error retrieving sessions ${error.message}`);
return null;
});
};

const run = async () => {
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));

let browser = null;

try {
browser = await puppeteer.connect({
browserWSEndpoint: `wss://YOUR_CONTAINER_URL_HERE?token=${apiToken}&id=${id}`,
});

const page = await browser.newPage();
await page.goto("https://example.com");

const [session] = await getSessionsById(id);

if (!session) {
throw new Error(`Error retrieving session!`);
}

console.log(`Open: ${session.devtoolsFrontendUrl} in your browser!`);

// Let the page stay open for 30 seconds so we can debug!
await sleep(30000);
} catch (error) {
console.error(`Saw error when running: ${error.message}`);
} finally {
if (browser) {
console.log(`Shutting down the browser.`);
browser.close();
}
}
};

run();

The examples include a page.waitFor call to give you time to visit the debugging page. In production, page interactions happen quickly and you may miss the session. To watch the script from the start, add the &pause query-string parameter to your puppeteer.connect call.

Selecting Modules

The examples use puppeteer or playwright, but you can use any library including Selenium. Puppeteer and Playwright have the best support.

The underlying GraphQL query retrieves session properties. Only devtoolsFrontendUrl is used in these examples. The other fields are included for reference.

query getSessions($id: String!, $apiToken: String!) {
sessions(apiToken: $apiToken, id: $id) {
description
devtoolsFrontendUrl
id
title
url
id
browserId
browserWSEndpoint
}
}

This query retrieves sessions by API token and id. The response looks like:

{
"data": {
"sessions": [
{
"id": null,
"initialConnectURL": "wss://YOUR_CONTAINER_URL_HERE/firefox/playwright/?token=YOUR_API_TOKEN_HERE",
"isTempDataDir": true,
"launchOptions": {},
"numbConnected": 1,
"routePath": ["/firefox/playwright", "/firefox/playwright"],
// ...
}
]
}
}

In all of our returned links we omit your API token, so you'll need to add it to the link manually to visit it. This is done for security purposes.

For questions or help with setup, contact us.