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 WebSocketTransport

From Leeroopedia

Template:Implementation Page

Overview

WebSocketTransport implements the ConnectionTransport interface over WebSocket connections, providing the primary transport mechanism for connecting to browser DevTools endpoints.

Description

The module defines the core transport types and the WebSocketTransport class. It establishes WebSocket connections to browser debugging endpoints with configurable headers, timeout, and per-message deflate compression. The class handles message serialization/deserialization and connection lifecycle.

Key types defined:

  • ProtocolRequest -- outgoing protocol command with id, method, params, sessionId
  • ProtocolResponse -- incoming response or event with optional error, result, params
  • ConnectionTransport -- interface contract for all transport implementations

The module also exports perMessageDeflate configuration for WebSocket compression.

Usage

Used to connect to browser DevTools endpoints (CDP, Juggler, WebKit inspector).

Code Reference

Source Location

packages/playwright-core/src/server/transport.ts (215 lines)

Class Signature

export interface ConnectionTransport {
  send(s: ProtocolRequest): void;
  close(): void;
  onmessage?: (message: ProtocolResponse) => void;
  onclose?: (reason?: string) => void;
}

export class WebSocketTransport implements ConnectionTransport {
  static connect(progress: Progress | null, url: string, headers?: HeadersArray, followRedirects?: boolean): Promise<WebSocketTransport>
  constructor(ws: WebSocket)
  send(message: ProtocolRequest): void
  close(): void
  onmessage?: (message: ProtocolResponse) => void
  onclose?: (reason?: string) => void
}

Import

import { WebSocketTransport, perMessageDeflate } from './server/transport';
import type { ConnectionTransport, ProtocolRequest, ProtocolResponse } from './server/transport';

I/O Contract

Inputs

  • url: string -- WebSocket URL to connect to
  • headers: HeadersArray -- optional connection headers
  • followRedirects: boolean -- whether to follow HTTP redirects

Outputs

  • Dispatches parsed ProtocolResponse objects via onmessage
  • Fires onclose when the WebSocket connection closes

Related Pages

Page Connections

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