Skip to content

feat: stream compilation output to browser in real time #49

Closed

Description

@zackees

Goal

Stream the Python compilation subprocess output (stdout/stderr) to the browser in real time via the Rust HTTP server. The user should see live build logs in the loading page, which then gets replaced by the compiled sketch output (index.html) when compilation succeeds.

Current behavior

  1. Rust HTTP server starts, browser opens and sees the loading page
  2. Loading page shows a spinner and "Compiling..." text
  3. Loading page polls `/build-status.json` every 500ms
  4. Python subprocess runs with inherited stdio (output goes to terminal only)
  5. On success, `build-status.json` is updated to `"success"`, loading page reloads
  6. Browser now shows the compiled sketch (`index.html`)

Problem: The browser shows no build progress. The user only sees a spinner. All compilation output goes to the terminal.

Proposed behavior

  1. Rust HTTP server starts, browser opens and sees the loading page
  2. Loading page subscribes to `/build-stream` SSE (Server-Sent Events) endpoint
  3. Python subprocess output is captured line-by-line and:
    • Displayed in the terminal (preserved behavior)
    • Pushed to the browser via SSE events
  4. Loading page renders build output in a scrolling log panel in real time
  5. On compilation success, the loading page smoothly transitions to the compiled sketch

Technical approach

Rust server: SSE endpoint

Add a `/build-stream` SSE endpoint to `server.rs`:

```
GET /build-stream -> text/event-stream

Events:
event: log
data: {"line": "Building sketch...", "stream": "stdout"}

event: status
data: {"status": "compiling", "message": "Compiling..."}

event: status
data: {"status": "success", "message": "Done"}

event: status
data: {"status": "error", "message": "Compilation failed"}
```

Rust server: shared build state

Add a shared state channel to `AppState`:

  • Use `tokio::sync::broadcast` channel for SSE fan-out (multiple browser tabs)
  • `compile_and_serve()` sends log lines and status events through the channel
  • SSE handler subscribes to the broadcast channel and streams events

Python subprocess: line-by-line capture

Change `run_python_compile()` in `main.rs`:

  • Use `Command::new().stdout(Stdio::piped()).stderr(Stdio::piped())` instead of inherited stdio
  • Spawn reader threads for stdout/stderr that:
    1. Print each line to the terminal (`println!`/`eprintln!`)
    2. Send each line through the broadcast channel to the SSE endpoint

Loading page: SSE client

Update `LOADING_PAGE` in `server.rs`:

  • Replace polling with `EventSource` (SSE client)
  • Render log lines in the `#log` panel as they arrive
  • On `status: success` event, reload to show the compiled sketch
  • Fall back to polling if SSE is unavailable

Implementation phases

Phase 1: SSE infrastructure

  • Add `tokio::sync::broadcast` channel to `AppState`
  • Add `/build-stream` SSE endpoint to `server.rs`
  • Pass the broadcast sender to `compile_and_serve()`

Phase 2: Line-by-line subprocess capture

  • Modify `run_python_compile()` to capture stdout/stderr with piped I/O
  • Spawn reader threads that tee output to terminal + broadcast channel

Phase 3: Loading page SSE client

  • Update `LOADING_PAGE` HTML/JS to use `EventSource`
  • Render log lines in a scrolling panel
  • Smooth transition to compiled output on success

Phase 4: Watch mode integration

  • On recompilation, send `status: compiling` event
  • Stream recompilation logs
  • Send `status: success` to trigger browser reload

Acceptance criteria

  • Browser shows live compilation output line-by-line
  • Terminal still shows the same output (no regression)
  • Multiple browser tabs can subscribe to the same stream
  • On success, loading page transitions to the compiled sketch
  • On error, loading page shows the error output
  • Watch mode recompilation streams new logs
  • Falls back to polling if EventSource is unsupported
  • Existing tests pass

Activity

  1. zackees commented on Apr 12, 2026

    @zackees
    MemberAuthor

    Completed in PR #50 — SSE endpoint streams build output to the browser loading page in real time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions