LightCDP.Connection (light_cdp v0.2.1)

Copy Markdown View Source

WebSocket client for the Chrome DevTools Protocol.

Manages a single WebSocket connection to a CDP endpoint, dispatching commands with auto-incrementing IDs and routing responses/events back to callers.

Typically not used directly — LightCDP.start/1 handles connection setup.

Low-level usage

{:ok, conn} = LightCDP.Connection.open("http://127.0.0.1:9222")
{:ok, result} = LightCDP.Connection.send_command(conn, "Browser.getVersion")
LightCDP.Connection.close(conn)

Summary

Functions

Blocks until the event registered with register_event_waiter/2 fires.

Closes the WebSocket connection.

Connects to a CDP endpoint.

Registers a waiter for a CDP event. Returns a ref to pass to await_event/2.

Waits for a CDP event by method name.

Functions

await_event(ref, timeout \\ 15000)

Blocks until the event registered with register_event_waiter/2 fires.

Returns {:ok, params} or {:error, %LightCDP.TimeoutError{}}.

close(pid)

Closes the WebSocket connection.

open(endpoint)

Connects to a CDP endpoint.

Fetches the WebSocket URL from {endpoint}/json/version, then opens a WebSocket connection.

Returns {:ok, pid} or {:error, %LightCDP.ConnectionError{}}.

register_event_waiter(pid, method)

Registers a waiter for a CDP event. Returns a ref to pass to await_event/2.

Register before triggering the action that produces the event to avoid race conditions.

send_command(pid, method, params \\ %{}, timeout \\ 15000, session_id \\ nil)

Sends a CDP command and waits for the response.

Returns {:ok, result}, {:error, %LightCDP.CDPError{}}, or {:error, %LightCDP.TimeoutError{}}.

session_id is required for commands targeting a specific page/target (anything after Target.attachToTarget).

wait_for_event(pid, method, timeout \\ 15000)

Waits for a CDP event by method name.

Combines register_event_waiter/2 and await_event/2.