Skip to main content

Overview

Streaming enables real-time browser viewport preview via WebSocket. This supports “pair browsing” where a human can watch and interact alongside an AI agent, or simply view the browser state as commands are executed.

Quick Start

The stream server starts automatically when AGENT_BROWSER_STREAM_PORT is set and remains active for the lifetime of the daemon.

WebSocket Protocol

Connect to ws://localhost:<port> to receive viewport frames and send input events.

Receive Frames

The server sends JPEG frames as JSON messages:
Frames are broadcast to all connected clients. Decode the base64 data to display the image.

Send Mouse Events

Event types: mousePressed, mouseReleased, mouseMoved, mouseWheel Buttons: left, right, middle, none

Send Keyboard Events

Event types: keyDown, keyUp, char

Send Touch Events

Event types: touchStart, touchEnd, touchMove, touchCancel

Request Status

Response:

Error Messages

Implementation Details

Automatic Screencast

When the first client connects, the stream server automatically starts screencasting (see src/stream-server.ts:203-209):
When the last client disconnects, screencasting stops automatically to conserve resources (see src/stream-server.ts:226-231).

Frame Quality

Default screencast options from src/stream-server.ts:364-370:
  • Format: JPEG (smaller size, faster transmission)
  • Quality: 80 (good balance between quality and size)
  • Max dimensions: 1280x720 (scales down larger viewports)
  • Frame rate: Every frame (set everyNthFrame: 2 to sample every other frame)

Input Injection

Input events are injected via CDP (Chrome DevTools Protocol). From src/stream-server.ts:244-275:
See src/browser.ts:2164-2228 for the CDP input injection implementation.

Security

Localhost Binding

The stream server binds to 127.0.0.1 only to prevent network exposure (from src/stream-server.ts:120-122):
Critical: The stream server allows direct input injection (mouse, keyboard, touch) which would be a critical security risk if exposed to the network.

Origin Validation

The stream server rejects cross-origin WebSocket connections from untrusted origins. From src/stream-server.ts:10-30:
Allowed origins:
  • No origin (CLI tools, WebSocket libraries)
  • file:// (local HTML viewers)
  • localhost, 127.0.0.1, ::1 (local web servers)
Rejected origins:
  • All other origins (prevents malicious web pages from connecting)

Stream Port Discovery

The daemon writes the stream port to a file for clients to discover (from src/daemon.ts:365-366):
Clients can read ~/.agent-browser/default.stream (or <session>.stream for named sessions) to discover the port.

Programmatic API

For advanced use, control streaming directly via the BrowserManager API:
See the TypeScript definitions in src/browser.ts:50-72 for full API details.

Example: Simple Viewer

Here’s a minimal HTML viewer for the stream:
Open this HTML file in a browser to view and interact with the stream.

Use Cases

AI Agent Monitoring

Watch an AI agent in real-time as it navigates and interacts with pages:

Pair Browsing

Human and AI collaborate - AI automates, human observes and corrects:

Debugging

Debug automation issues by watching the actual browser state:

Recording

Record browser sessions by capturing stream frames:

Limitations

iOS Not Supported

Streaming requires CDP (Chrome DevTools Protocol), which is not available for iOS Safari automation. From src/ios-actions.ts:259:

Performance

Streaming adds overhead:
  • Each frame is JPEG-encoded and base64-encoded
  • Network transmission of potentially large frames
  • Multiple clients multiply the bandwidth
For production automation, disable streaming unless you need live preview.

Frame Rate

The stream captures every rendered frame by default. For slower connections, reduce frame rate:

Troubleshooting

Connection Refused

Solutions:
  1. Verify AGENT_BROWSER_STREAM_PORT is set before launching the daemon
  2. Check if the daemon is running: pgrep -f agent-browser
  3. Verify the port is listening: lsof -i :9223 (macOS/Linux) or netstat -ano | findstr :9223 (Windows)

No Frames Received

Possible causes:
  1. Browser not launched yet - send a command first: agent-browser open example.com
  2. Screencast failed to start - check for errors in daemon logs
  3. Multiple clients - frames are broadcast, ensure at least one client is connected

Origin Blocked

The stream server only accepts connections from localhost and file:// origins. Host your viewer on localhost or use a file:// URL.