From 6f26d222a3cdb5df688c72d8957f2b1e05e1f765 Mon Sep 17 00:00:00 2001 From: Mustafa Yenler <94706243+Musti7even@users.noreply.github.com> Date: Wed, 25 Nov 2026 23:56:08 +0000 Subject: [PATCH] examples: app-builder preview in one call (POST /v1/previews) phoneFor(), load() and the embed call become one POST /v1/previews: the end user's phone, the APK installed or the Expo link opened, on screen, the embed link. Stage errors (install_failed, open_failed) reach the page. README: one call in the steps and the table, idempotency keys, the limits request no longer promises an answer, and a new recorded run against production (27 s new phone with an APK, 41 s with Expo, 15 s from parked). Co-Authored-By: Claude Opus 5.5 --- examples/app-builder-preview/README.md | 29 +++++++++--------- examples/app-builder-preview/index.html | 4 +-- examples/app-builder-preview/server.mjs | 39 ++++++------------------- 3 files changed, 25 insertions(+), 47 deletions(-) diff --git a/examples/app-builder-preview/README.md b/examples/app-builder-preview/README.md index 6b29ef6..9cbffe6 100644 --- a/examples/app-builder-preview/README.md +++ b/examples/app-builder-preview/README.md @@ -1,15 +1,15 @@ # App-builder preview -Show each of your users their app on a cloud Android phone, live in your own page. The whole integration is two files and no dependencies: `server.mjs` (about 150 lines) and `index.html`. +Show each of your users their app on a cloud Android phone, live in your own page. The whole integration is two files and no dependencies: `server.mjs` (about 120 lines) and `index.html`. A preview is one call, `POST /v1/previews`. ![The example running against production: an APK and an Expo app previewed for two end users, webhooks arriving, usage per end user](demo.gif) What it does: 1. **Keeps your API key on the server.** The browser only ever gets a short-lived embed URL. -2. **One phone per end user.** It creates the phone with `end_user` (your id for the person) and `display_hz: 30` for smooth scrolling, reuses it next time and starts it again if it's parked. -3. **Loads the app.** An APK URL installs and opens. An Expo link (`exp://`, `exps://`, `https://expo.dev/…`) opens in Expo Go, and Mobilebox installs the Expo Go that matches the project's SDK. -4. **Embeds the live phone** through `/embed`, locked to your page's origin. Your user can tap, swipe and type on it. +2. **One call per preview.** `POST /v1/previews` with `end_user` (your id for the person) and the build gets that user's phone (theirs again, started if parked, or a new one with a 30 Hz screen), installs the APK, or opens an Expo link (`exp://`, `exps://`, `https://expo.dev/…`) in the Expo Go that matches the project's SDK, and answers once the app is on screen. +3. **Embeds the live phone** with the `embed_url` from that answer, locked to your page's origin. Your user can tap, swipe and type on it. +4. **Says what went wrong.** A build that doesn't install or open comes back as `install_failed` or `open_failed` with the reason, and the page shows it. 5. **Receives signed webhooks** (`phone.ready`, `phone.parked`, `phone.deleted`, `phone.failed`), checks the signature and shows each phone's state. 6. **Shows usage per end user**, the minutes and cost you can bill them for. @@ -45,9 +45,7 @@ A workspace has one webhook URL, so if yours already points somewhere else the s | Step | Code | Mobilebox API | |---|---|---| -| A phone per end user | `phoneFor()` | `GET /v1/phones?end_user=…`, `POST /v1/phones {end_user, display_hz: 30}`, `POST /v1/phones/{id}/start` | -| The app on it | `load()` | `POST /v1/phones/{id}/apps {url}` + `open_app`, or `open_url` for Expo | -| The live phone in your page | `/api/preview` | `POST /v1/phones/{id}/embed {origin, interactive: true, ttl: 900}` | +| The end user's phone, the app on screen, the link | `preview()` | `POST /v1/previews {apk_url or expo_url, end_user, origin, interactive: true, ttl: 900}` | | Phone state | `verify()`, `onWebhook()` | `PUT /v1/webhooks {url}`; events signed `Mobilebox-Signature: t=…,v1=…` | | Usage per end user | `/api/usage` | `GET /v1/account/usage?by=end_user` | | Done | `/api/park` | `DELETE /v1/phones/{id}/embed`, `POST /v1/phones/{id}/park` | @@ -60,7 +58,8 @@ A workspace has one webhook URL, so if yours already points somewhere else the s - **Answer webhooks fast.** Return 2xx within 10 s and do the work after. We retry after 10 s, 1 min, 10 min and 1 h. - **Keep embed links short-lived.** Make a new link when one ends (the iframe tells your page with `mobilebox:end`). Revoke the link when your user leaves. - **Park idle phones.** A parked phone keeps its apps and data and costs nothing. Phones also park by themselves after their idle timeout. -- **Plan for more users.** A workspace runs 3 phones at once to start. `POST /v1/account/limits {"running": n}` asks for more, and we answer within a day. +- **Plan for more users.** A workspace runs 3 phones at once to start. `POST /v1/account/limits {"running": n}` asks for more; `GET /v1/account` shows your limits. +- **Send an idempotency key** (`Idempotency-Key` header) with each preview if your server retries: a retry gets the same preview, never a second phone. The page polls `/api/events` every 2 s instead of holding a server-sent event stream, because tunnels and proxies, Cloudflare's quick tunnels among them, often hold such a stream back. @@ -68,14 +67,14 @@ Reference: [embed](https://mobilebox.dev/docs/embed), [Expo](https://mobilebox.d ## Recorded run -**2026-11-25**, Node 24 on Linux, against production through a Cloudflare quick tunnel, Chrome: +**2026-11-25**, Node 24 on Linux, against production through a Cloudflare quick tunnel, Chrome, one `POST /v1/previews` per Preview: | Step | Result | Time | |---|---|---| -| Preview, `user_42`, F-Droid Editor 1.98 APK, new phone | installed, live in the page at 30 Hz, a click in the page reached the phone | 29 s | -| Park phone | `phone.parked` (manual) arrived signed; usage: user_42 2.2 min, $0.02 | 2 s | -| Preview again, `user_42` | parked phone started, `phone.ready`, app reinstalled, live | 15 s | -| Preview, `user_7`, Expo SDK 58 project (`exps://…exp.direct`), new phone | Expo Go 58.0.6 installed itself, app live; an edit showed by Fast Refresh in the embed | 30 s | -| Park phone, `user_7` | `phone.parked` arrived, the embed showed "This preview was closed", usage: user_7 1 min, $0.01 | 2 s | +| Preview, `user_43`, F-Droid Editor 1.98 APK, new phone | installed and on screen when the call returned, live in the page | 27 s | +| Preview, `user_8`, Expo SDK 58 project (`exp://…exp.direct`), new phone | Expo Go 58.0.6 installed itself, the app's first screen live in the page | 41 s | +| Park both | `phone.parked` (manual) arrived signed for each; usage: user_43 1.5 min, $0.02; user_8 1 min, $0.01 | 2 s | +| Preview again, `user_43` | the parked phone started (`reused: true`, `phone.ready`), app on screen | 15 s | +| Preview, `user_43`, an APK link that answers 404 | `install_failed: The app didn't install: f-droid.org answered 404.`, the same phone kept | 0.2 s | -An unsigned POST to the webhook URL got 401. Expo Go shows its developer menu once on a new phone; your user closes it in the embed. +Times include the tunnel. Expo Go shows its developer menu once on a new phone; your user closes it in the embed. diff --git a/examples/app-builder-preview/index.html b/examples/app-builder-preview/index.html index 5ff5bdf..e7a7b7d 100644 --- a/examples/app-builder-preview/index.html +++ b/examples/app-builder-preview/index.html @@ -72,7 +72,7 @@

Usage per end user, last 30 days