Skip to content

Repository files navigation

🌊 Browser Pool

Robust browser session management for Playwright & Browser.cash.

Powered by Browser.cash remote browsers.

FeaturesInstallationUsageConfigurationContributing

LicenseNode.js VersionTypeScriptVisit Browser.cash

Follow on XFollow on LinkedInJoin our Discord


💡 Pro Tip: See this library in action in Teracrawl and Browser SERP.


🚀 What is Browser Pool?

Browser Pool is a specialized library designed to manage pools of remote browser sessions. It handles the lifecycle of Playwright browsers connected to Browser.cash, ensuring your application always has a healthy browser ready to perform tasks.

It abstracts away the complexity of connection management, error recovery, and session recycling, making it ideal for building high-concurrency scrapers and automation tools.

✨ Features

  • Automatic Pooling: Maintains a fixed number of active browser sessions.
  • Target-Aware Pooling: Specify exactly how many browsers you want per node, country, or node type.
  • Self-Healing: Automatically detects and replaces dead or disconnected browsers.
  • Health Checks: Periodically verifies browser responsiveness.
  • Concurrency Control: Queues requests when all sessions are busy.
  • Type-Safe: Written in TypeScript with full type definitions.

🛠️ Installation

npm install @browsercash/pool

Note: You must also have playwright-core installed as a peer dependency.

💻 Usage

import{chromium}from"playwright-core";import{SessionPool,typePoolConfig}from"@browsercash/pool";constcfg: PoolConfig={apiKey: process.env.BROWSER_API_KEY,
chromium,targets: [{id: "us-hosted",count: 2,country: "US",type: "hosted"},{id: "de-consumer",count: 1,country: "DE",type: "consumer_distributed"},{id: "pinned-node",count: 1,nodeId: "node_123"},],enableHealthCheck: true,waitQueueTimeoutMs: 30_000,};// 1. Create the poolconstpool=newSessionPool(cfg);// 2. Initializeawaitpool.init();// 3. Acquire a session (waits if none available)constsession=awaitpool.acquire();try{// Use the standard Playwright browser instanceconstpage=awaitsession.browser.newPage();awaitpage.goto("https://example.com");console.log(awaitpage.title());}finally{// 4. Always release the session back to the pool// Pass 'true' as second arg if the session encountered a fatal errorpool.release(session);}// 5. Cleanup on shutdownawaitpool.shutdown();

There is also a dedicated example config file in examples/browser-pool.config.mjs.

If you just want the old behavior, size still works:

constpool=newSessionPool({apiKey: process.env.BROWSER_API_KEY,
chromium,size: 3,});

Example CFG

import{chromium}from"playwright-core";import{typePoolConfig}from"@browsercash/pool";exportconstbrowserPoolCfg: PoolConfig={apiKey: process.env.BROWSER_API_KEY!,
chromium,targets: [{id: "us-primary",count: 2,country: "US",type: "hosted"},{id: "eu-fallback",count: 2,country: "DE",type: "consumer_distributed"},{id: "pinned-fraud-node",count: 1,nodeId: "node_123"},],maxUses: 25,maxAgeMs: 10*60*1000,maxIdleMs: null,enableHealthCheck: true,healthCheckIntervalMs: 30_000,sessionReadyTimeoutMs: 30_000,cdpConnectTimeoutMs: 15_000,waitQueueTimeoutMs: 60_000,};

Equivalent standalone file:

// examples/browser-pool.config.mjsimport{chromium}from"playwright-core";exportconstbrowserPoolCfg={apiKey: process.env.BROWSER_API_KEY,
chromium,targets: [{id: "de-consumer",count: 1,country: "DE",type: "consumer_distributed"},],maxUses: 25,maxAgeMs: 10*60*1000,maxIdleMs: null,enableHealthCheck: true,healthCheckIntervalMs: 30_000,sessionReadyTimeoutMs: 30_000,cdpConnectTimeoutMs: 15_000,waitQueueTimeoutMs: 60_000,};

For a larger production mix, expand targets with more entries such as hosted US nodes or pinned nodeId targets.

⚙️ Configuration

OptionTypeDefaultDescription
apiKeystringRequiredYour Browser.cash API key.
chromiumChromiumModuleRequiredThe Playwright Chromium module.
sizenumber1Backward-compatible shortcut for a single default target group.
targetsPoolTarget[][]Explicit target mix. Sum of count values becomes the pool size.
maxUsesnumber50Max times a browser is reused before recycling.
maxAgeMsnumber300000Max age (ms) of a session before recycling.
maxIdleMsnumber | nullnullMax idle time before recycling. null or 0 disables idle recycling.
enableHealthCheckbooleanfalseEnable background remote health checks.
healthCheckIntervalMsnumber30000Interval for health checks (ms).
healthCheckTimeoutMsnumbermin(interval, 10000)Timeout for each health-check request.
sessionReadyTimeoutMsnumber20000How long to wait for Browser.cash to return a usable CDP URL.
cdpConnectTimeoutMsnumber15000Timeout for Playwright connectOverCDP.
enableWaitQueuebooleantrueQueue acquire requests if pool is full.
waitQueueTimeoutMsnumber60000Max time an acquire call will wait in queue.
enableDisconnectHandlingbooleantrueReplace sessions when the CDP connection disconnects.
createPagebooleanfalsePre-create a page for each pooled session.
debugbooleanfalseEnable verbose logging.
logger(message, data) => voidconsole.logCustom logger used when debug is enabled.

PoolTarget

typePoolTarget={id?: string;count: number;nodeId?: string;country?: string;type?: "consumer_distributed"|"hosted";sessionOptions?: Record<string,unknown>;};
  • Use count to pin how many sessions should exist for that target.
  • Use nodeId when you want a specific Browser.cash node.
  • Use country and type when you want the pool to maintain a regional mix.
  • Use sessionOptions as a pass-through for newer Browser.cash session-create fields.

🤝 Contributing

Contributions are welcome! We appreciate your help in making Browser Pool better.

How to Contribute

  1. Fork the Project: click the 'Fork' button at the top right of this page.
  2. Create your Feature Branch: git checkout -b feature/AmazingFeature
  3. Commit your Changes: git commit -m 'Add some AmazingFeature'
  4. Push to the Branch: git push origin feature/AmazingFeature
  5. Open a Pull Request: Submit your changes for review.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Session management library for Playwright remote browsers. Handles pooling, health checks, and auto-recovery for high-frequency scraping.

Topics

Resources

Stars

9 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages