Repository navigation
Description
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
- Rust HTTP server starts, browser opens and sees the loading page
- Loading page shows a spinner and "Compiling..." text
- Loading page polls `/build-status.json` every 500ms
- Python subprocess runs with inherited stdio (output goes to terminal only)
- On success, `build-status.json` is updated to `"success"`, loading page reloads
- 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
- Rust HTTP server starts, browser opens and sees the loading page
- Loading page subscribes to `/build-stream` SSE (Server-Sent Events) endpoint
- Python subprocess output is captured line-by-line and:
- Displayed in the terminal (preserved behavior)
- Pushed to the browser via SSE events
- Loading page renders build output in a scrolling log panel in real time
- 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:
- Print each line to the terminal (`println!`/`eprintln!`)
- 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
feat: stream compilation output to browser in real time via SSE (#49)
feat: stream compilation output to browser in real time via SSE (#49) (…
zackees commentedon Apr 12, 2026
Completed in PR #50 — SSE endpoint streams build output to the browser loading page in real time.