Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Playwright FfConnection

From Leeroopedia

Template:Implementation Page

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 messages
  • browserLogsCollector: RecentLogsCollector -- collector for browser log output

Outputs

  • Dispatches protocol events to registered FFSession instances
  • Emits ConnectionEvents.Disconnected when 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

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment