Implementation:Microsoft Playwright BidiConnection
| Knowledge Sources | |
|---|---|
| Domains | BiDi, Protocol |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for managing the WebDriver BiDi protocol connection and session multiplexing provided by the Playwright library.
Description
The `BidiConnection` class manages the raw WebDriver BiDi protocol transport, dispatching incoming messages to the appropriate `BidiSession`. It maintains a browser-level session and per-browsing-context sessions via `_browsingContextToSession` and `_realmToBrowsingContext` maps. The companion `BidiSession` class (defined in the same module) handles sending typed commands with `send()`, event subscription, and callback management. Special message IDs (`kBrowserCloseMessageId`, `kShutdownSessionNewMessageId`) are reserved for browser close and session shutdown commands.
Usage
Use BidiConnection when establishing and managing a BiDi protocol connection to Firefox or Chromium-via-BiDi browsers in Playwright's server-side browser automation.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/bidi/bidiConnection.ts
Signature
export const kBrowserCloseMessageId = Number.MAX_SAFE_INTEGER - 1;
export const kShutdownSessionNewMessageId = kBrowserCloseMessageId - 1;
export class BidiConnection {
readonly browserSession: BidiSession;
readonly _browsingContextToSession: Map<string, BidiSession>;
readonly _realmToBrowsingContext: Map<string, string>;
readonly _realmToOwnerRealm: Map<string, string>;
constructor(transport: ConnectionTransport, onDisconnect: () => void, protocolLogger: ProtocolLogger, browserLogsCollector: RecentLogsCollector);
}
export class BidiSession extends EventEmitter {
readonly sessionId: string;
constructor(connection: BidiConnection, sessionId: string, rawSend: (message: any) => void);
async send<T extends keyof bidiCommands.Commands>(method: T, params: bidiCommands.Commands[T]['params']): Promise<bidiCommands.Commands[T]['returnType']>;
}
Import
import { BidiConnection, BidiSession, kBrowserCloseMessageId } from '../server/bidi/bidiConnection';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| transport | ConnectionTransport | Yes | The underlying message transport (WebSocket or pipe) |
| onDisconnect | () => void | Yes | Callback invoked when the connection closes |
| protocolLogger | ProtocolLogger | Yes | Logger for protocol message tracing |
| browserLogsCollector | RecentLogsCollector | Yes | Collector for browser process logs |
Outputs
| Name | Type | Description |
|---|---|---|
| browserSession | BidiSession | The root browser-level BiDi session |
| response | Commands[T]['returnType'] | Typed response from a BiDi command |
Usage Examples
import { BidiConnection } from '../server/bidi/bidiConnection';
const connection = new BidiConnection(transport, () => console.log('disconnected'), logger, logsCollector);
const result = await connection.browserSession.send('browser.createUserContext', {});