Implementation:Microsoft Playwright WkConnection
Overview
WKConnection manages the WebSocket-based connection between Playwright and the WebKit browser process, dispatching protocol messages to sessions and handling page proxy multiplexing.
Description
The WKConnection class manages the protocol communication layer for WebKit automation. Unlike Firefox's session-based multiplexing, WebKit uses page proxy IDs to route messages to the correct page. The connection maintains a browser-level session and dispatches incoming messages based on pageProxyId fields.
The companion WKSession class handles individual protocol sessions, providing typed send() methods and event dispatching. A special kPageProxyMessageReceived symbol event unifies page proxy lifecycle management.
Usage
Created internally by the WebKit browser type when launching or connecting to a WebKit browser.
Code Reference
Source Location
packages/playwright-core/src/server/webkit/wkConnection.ts (173 lines)
Class Signature
export class WKConnection {
readonly browserSession: WKSession;
constructor(transport: ConnectionTransport, onDisconnect: () => void, protocolLogger: ProtocolLogger, browserLogsCollector: RecentLogsCollector)
rawSend(message: any): void
close(): void
}
export class WKSession extends EventEmitter {
readonly sessionId: string;
send<T extends keyof Protocol.CommandParameters>(method: T, params?: Protocol.CommandParameters[T]): Promise<Protocol.CommandReturnValues[T]>
dispatchMessage(message: any): void
dispose(): void
}
Import
import { WKConnection, WKSession, kBrowserCloseMessageId, kPageProxyMessageReceived } from './server/webkit/wkConnection';
I/O Contract
Inputs
transport: ConnectionTransport-- WebSocket or pipe transportonDisconnect: () => void-- disconnect callbackprotocolLoggerandbrowserLogsCollectorfor logging
Outputs
- Dispatches protocol events to
WKSessioninstances - Emits
kPageProxyMessageReceivedfor page proxy messages
Constants
kBrowserCloseMessageId = -9999-- special close message IDkPageProxyMessageReceived-- symbol for unified page proxy events
Related Pages
- Microsoft_Playwright_WebKit_BrowserType -- WebKit browser type creating connections
- Microsoft_Playwright_WkExecutionContext -- Execution context using WKSession
- Microsoft_Playwright_FfConnection -- Firefox equivalent