A new browser for each task
Every task waits for a browser to start and pays for the start in CPU time.
BrowserThing is a self-hosted browser pool for Playwright applications. It keeps browsers running between short tasks, cleans up each session, and replaces each browser after a set number of sessions.
Your applications connect to one endpoint. Each worker keeps one browser running and gives every session its own context. After 50 sessions, a worker drains and starts a new browser.
Applications that run many short browser tasks, such as screenshots, PDFs, or checks, usually start with one of the first two approaches.
Every task waits for a browser to start and pays for the start in CPU time.
Memory use can grow over time. If the browser gets into a bad state, it stays there until someone restarts it.
Tasks use browsers that are already running. Each browser is replaced after a set number of sessions.
Change launch() to connect() with the BrowserThing URL. The browser steps stay in your application. BrowserThing selects a worker for the browser type and Playwright version, and relays the session.
import { chromium } from 'playwright';
const browser = await chromium.connect('ws://localhost:8080', {
headers: { Authorization: `Bearer ${process.env.BROWSERTHING_API_KEY}` },
});
try {
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://example.com');
await page.screenshot({ path: 'preview.png' });
} finally {
await browser.close();
}import os
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.connect(
"ws://localhost:8080",
headers={"Authorization": f"Bearer {os.environ['BROWSERTHING_API_KEY']}"},
)
try:
context = browser.new_context()
page = context.new_page()
page.goto("https://example.com")
page.screenshot(path="preview.png")
finally:
browser.close()Run workers on their own hosts. Add workers when you need more sessions, without more application instances.
Each session uses its own browser context. The worker removes it when the connection closes.
After 50 sessions by default, a worker stops new sessions, lets active ones finish, and starts a new browser.
When every matching slot is busy, new connections can wait for a free slot instead of failing.
A REST API shows workers, sessions, and free capacity, so you can see the pool and plan for it.
Every client and worker uses an API key. Keys are stored as hashes in PostgreSQL.
You need Docker with the Compose plugin and Node.js 20 or later.
curl -fsSLO https://mbroton.github.io/browserthing/downloads/docker-compose.yaml
curl -fsSL --create-dirs -o worker/seccomp_profile.json \
https://raw.githubusercontent.com/mbroton/browserthing/v0.10.0/worker/seccomp_profile.jsondocker compose up -d --wait server
export BROWSERTHING_API_KEY=$(docker compose exec -T server server apikey create --name quickstart)
echo "WORKER_API_KEY=$BROWSERTHING_API_KEY" >> .env
docker compose up -dnpm install playwright@1.65.0