Implementation:Microsoft Playwright PlaywrightServer
| Knowledge Sources | |
|---|---|
| Domains | Remote Execution, WebSocket Server |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for hosting a remote Playwright WebSocket server provided by the Playwright library.
Description
The `PlaywrightServer` class manages a WebSocket-based server that allows remote clients to connect and control browser instances. It supports multiple operational modes: `default` (full Playwright access), `launchServer` (single browser launch), `launchServerShared` (shared browser reuse), and `extension` (for browser extension development). The server uses a `WSServer` for WebSocket handling, a `Semaphore` for connection limiting, and can accept pre-launched browsers, Android devices, or SOCKS proxies. Browser reuse logic prevents disposing browsers that should persist across connections.
Usage
Use this class when you need to run Playwright in a server mode that accepts remote WebSocket connections, such as when running browsers on a remote machine, sharing browser instances across test workers, or enabling the Playwright extension development workflow.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/remote/playwrightServer.ts
Signature
type ServerOptions = {
path: string;
maxConnections: number;
mode: 'default' | 'launchServer' | 'launchServerShared' | 'extension';
preLaunchedBrowser?: Browser;
preLaunchedAndroidDevice?: AndroidDevice;
preLaunchedSocksProxy?: SocksProxy;
};
export class PlaywrightServer {
private _playwright: Playwright;
private _options: ServerOptions;
private _wsServer: WSServer;
private _dontReuseBrowsers: Set<Browser>;
constructor(options: ServerOptions);
async listen(port?: number): Promise<string>;
async close(): Promise<void>;
}
Import
import { PlaywrightServer } from 'playwright-core/lib/remote/playwrightServer';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| options.path | string | Yes | WebSocket endpoint path |
| options.maxConnections | number | Yes | Maximum number of concurrent WebSocket connections |
| options.mode | string | Yes | Server operating mode (default, launchServer, launchServerShared, extension) |
| options.preLaunchedBrowser | Browser | No | Pre-launched browser instance to serve |
| options.preLaunchedAndroidDevice | AndroidDevice | No | Pre-launched Android device to serve |
| options.preLaunchedSocksProxy | SocksProxy | No | Pre-launched SOCKS proxy for network routing |
| port | number | No | TCP port to listen on (passed to listen method) |
Outputs
| Name | Type | Description |
|---|---|---|
| wsEndpoint | string | The WebSocket URL that clients can connect to |
Usage Examples
import { PlaywrightServer } from 'playwright-core/lib/remote/playwrightServer';
const server = new PlaywrightServer({
path: '/ws',
maxConnections: 10,
mode: 'default',
});
const wsEndpoint = await server.listen(3000);
console.log('Playwright server listening at:', wsEndpoint);
// Clients can now connect:
// const browser = await playwright.chromium.connect(wsEndpoint);
await server.close();