Implementation:Microsoft Playwright Client Connection
| Knowledge Sources | |
|---|---|
| Domains | Client-Server Communication, RPC Protocol |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for managing the client-to-server RPC connection provided by the Playwright library.
Description
The `Connection` class is the central hub of Playwright's client-side communication layer. It extends `EventEmitter` and manages the bidirectional message passing between the client and server processes. The class maintains a registry of all `ChannelOwner` objects (stored in `_objects` map), dispatches method calls to the server with callback tracking, and processes incoming messages that create, update, or dispose remote objects. It also handles protocol validation, error deserialization, and tracing state management. The inner `Root` class serves as the root of the object hierarchy and bootstraps the connection by initializing the `Playwright` instance.
Usage
Use this class to establish and manage the connection between a Playwright client and the Playwright server process. It is the entry point for all client-side RPC communication.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/client/connection.ts
Signature
export class Connection extends EventEmitter {
readonly _objects: Map<string, ChannelOwner>;
onmessage: (message: object) => void;
readonly _instrumentation: ClientInstrumentation;
readonly headers: HeadersArray;
constructor(platform: Platform, localUtils?: LocalUtils, instrumentation?: ClientInstrumentation, headers?: HeadersArray);
markAsRemote(): void;
isRemote(): boolean;
useRawBuffers(): void;
rawBuffers(): boolean;
localUtils(): LocalUtils | undefined;
}
Import
import { Connection } from './client/connection';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| platform | Platform | Yes | Platform abstraction for environment-specific operations |
| localUtils | LocalUtils | No | Local utility functions reference |
| instrumentation | ClientInstrumentation | No | Client instrumentation for tracing/debugging |
| headers | HeadersArray | No | HTTP headers to include with connection requests |
Outputs
| Name | Type | Description |
|---|---|---|
| _objects | Map<string, ChannelOwner> | Registry of all remote objects keyed by GUID |
| Playwright | Playwright | The initialized Playwright instance (via Root.initialize) |
Usage Examples
import { Connection } from 'playwright-core/lib/client/connection';
const connection = new Connection(platform, localUtils);
connection.onmessage = (message) => transport.send(message);
transport.onmessage = (message) => connection.dispatch(message);
const playwright = await connection.initializePlaywright();