Implementation:Microsoft Playwright FfConnection
Overview
FFConnection manages the WebSocket-based connection between Playwright and the Firefox browser process using the Juggler protocol. It multiplexes multiple sessions over a single transport, dispatching protocol messages to the appropriate FFSession instances.
Description
The FFConnection class extends EventEmitter and implements the connection layer for Firefox automation. It maintains a map of active FFSession objects keyed by session ID, with a special root session for browser-level commands. Messages received from the transport are dispatched to the correct session based on their sessionId field. The class handles connection lifecycle including disconnect detection and logging of protocol messages.
The companion FFSession class represents an individual protocol session within the connection, providing typed send() methods for issuing Firefox DevTools Protocol commands and receiving events.
Usage
FFConnection is instantiated internally by the Firefox browser type when launching or connecting to a Firefox browser instance.
Code Reference
Source Location
packages/playwright-core/src/server/firefox/ffConnection.ts (170 lines)
Class Signature
export class FFConnection extends EventEmitter {
constructor(transport: ConnectionTransport, protocolLogger: ProtocolLogger, browserLogsCollector: RecentLogsCollector)
readonly rootSession: FFSession;
readonly _sessions: Map<string, FFSession>;
}
export class FFSession extends EventEmitter {
send<T extends keyof Protocol.CommandParameters>(method: T, params?: Protocol.CommandParameters[T]): Promise<Protocol.CommandReturnValues[T]>
createSession(sessionId: string): FFSession
dispose(): void
}
Import
import { FFConnection, FFSession, ConnectionEvents, kBrowserCloseMessageId } from './server/firefox/ffConnection';
I/O Contract
Inputs
transport: ConnectionTransport-- underlying transport (WebSocket or pipe)protocolLogger: ProtocolLogger-- logger for protocol messagesbrowserLogsCollector: RecentLogsCollector-- collector for browser log output
Outputs
- Dispatches protocol events to registered
FFSessioninstances - Emits
ConnectionEvents.Disconnectedwhen the transport closes
Constants
kBrowserCloseMessageId = -9999-- special message ID used for browser close commands
Usage Examples
Creating a Connection
const connection = new FFConnection(transport, protocolLogger, browserLogsCollector);
const rootSession = connection.rootSession;
await rootSession.send('Browser.enable');
Creating a Child Session
const pageSession = rootSession.createSession(sessionId);
await pageSession.send('Page.enable');
Related Pages
- Microsoft_Playwright_FfExecutionContext -- Firefox execution context using FFSession
- Microsoft_Playwright_FfInput -- Firefox input handling via FFSession
- Microsoft_Playwright_FfNetworkManager -- Firefox network management
- Microsoft_Playwright_WebSocketTransport -- Transport layer used by FFConnection