Skip to content

IPC Model

Tynd’s IPC is intentionally boring: no HTTP, no WebSocket, no TCP port. Everything rides native WebView bindings + a pair of custom schemes.

Channel Direction Transport
api.<fn>() Frontend → Backend window.ipc.postMessage → stdin JSON
api.on("evt") Backend → Frontend Rust evaluate_script
events.emit() Backend → Frontend stdout JSON → Rust → evaluate_script
dialog / fs / http / … Frontend → Rust (OS APIs) window.ipc.postMessage (direct, no backend round-trip)
terminal:data / http:progress Rust → Frontend (stream) native user events → evaluate_script
fs.readBinary / compute.hash / … Frontend ↔ Rust (binary) tynd-bin:// custom scheme, raw bytes

No HTTP. No WebSocket. No firewall prompt.

A native custom protocol maps tynd://localhost/<path> to the built frontend directory (dev: proxied to the dev server; prod: read from the TYNDPKG-packed assets). The asset cache is pre-warmed on a background thread before the WebView is built, so the first request is instant.

GET tynd://localhost/index.html → reads dist/index.html
GET tynd://localhost/main.js → reads dist/main.js

window.location.origin is tynd://localhost. You can reason about same-origin exactly as on a normal HTTPS site.

backend/main.ts
export async function greet(name: string) {
return `Hello, ${name}!`;
}
// frontend
import { createBackend } from "@tynd/core/client";
import type * as backend from "../backend/main";
const api = createBackend<typeof backend>();
const msg = await api.greet("Alice"); // "Hello, Alice!"

Wire:

// frontend → rust (via postMessage)
{ "type": "call", "id": "c42", "fn": "greet", "args": ["Alice"] }
// Rust → Bun backend over framed stdin
{ "type": "call", "id": "c42", "fn": "greet", "args": ["Alice"] }
// backend → rust
{ "type": "return", "id": "c42", "value": "Hello, Alice!" }
// rust → frontend (via evaluate_script of the originating webview)

JSON IPC frames are capped at 8 MiB across the WebView, Rust host, and Bun transport. Oversized calls reject immediately instead of waiting for the frontend timeout. Use the binary APIs below for larger payloads.

If Bun exits unexpectedly, every in-flight backend call and stream rejects with code: "BACKEND_DEAD". This state stays active, so calls created after the crash also reject immediately without posting another IPC frame. A successful development reload installs a fresh document and shim, clearing the state. Controlled reloads use one shutdown marker per Bun generation; delayed EOF from the previous process cannot be reported as a crash of the new process.

The frontend only knows the backend by typeof backend — no generated files, no IDL, no .d.ts step. Rename a backend function and every stale frontend call lights up in the compiler.

const api = createBackend<typeof backend>();
// ^^^^^^^^^^^^^^^^ compile-time-only type import

createBackend is a thin Proxy that sends the call by method name. The runtime code is tiny (one postMessage + a Promise.withResolvers).

Typed via createEmitter<T>():

// backend
import { createEmitter } from "@tynd/core";
export const events = createEmitter<{
userCreated: { id: string; name: string };
}>();
events.emit("userCreated", { id: "1", name: "Alice" });
// frontend
api.on("userCreated", (user) => console.log(user.name)); // user: { id, name }
api.once("userCreated", (user) => { /* fires once */ });

Wire: { "type": "emit", "name": "userCreated", "data": {...} } lands on stdout, Rust reads the frame and evaluate_scripts __tynd_emit__(name, data) on every subscribed app-local webview (remote-content webviews never receive backend events).

Streaming RPC — async-generator handlers

Section titled “Streaming RPC — async-generator handlers”

If a backend export is an async function*, the frontend gets a StreamCall handle — awaitable and async-iterable:

// backend
export async function* processFiles(paths: string[]) {
let ok = 0;
for (const [i, path] of paths.entries()) {
await doWork(path);
ok++;
yield { path, progress: (i + 1) / paths.length };
}
return { ok, failed: paths.length - ok };
}
// frontend
const stream = api.processFiles(["a.txt", "b.txt"]);
for await (const chunk of stream) {
render(chunk.progress); // yields
}
const summary = await stream; // return value
// early stop: await stream.cancel();

Three flow-control mechanisms keep streaming safe at arbitrary yield rates:

  1. Per-stream credit — backend generator starts with 64 credits. Every yield decrements. At 0 credit, the backend awaits a waiter; the frontend replenishes by ACK-ing every 32 consumed chunks. Bounded memory regardless of producer speed.
  2. Yield batching — Rust buffers yields and flushes either every 10ms or at 64 items per bucket. One evaluate_script per webview per flush, not one per chunk.
  3. Cleanup on window close — when a secondary window closes, Rust cancels every active stream that originated there so generators don’t leak.

Combined guarantee: 10k+ yields/sec to the UI without freezing it.

See the Streaming RPC guide.

Native things bypass the TypeScript backend entirely:

Install the required plugin:

Terminal window
tynd add dialog
// frontend
import { dialog } from "@tynd/plugin-dialog/client";
const path = await dialog.openFile({ filters: [{ name: "Images", extensions: ["png", "jpg"] }] });

Wire: frontend posts { type: "os_call", api: "dialog", method: "openFile", args: [...] }. The native host validates and dispatches the call to the selected Rust API.

Backend never sees OS calls — the TypeScript backend is only for your app logic, not for bridging the frontend to the OS.

  • tyndWindow.* calls run on the main thread (event loop proxy) because native windowing requires it. Each request carries the label of the webview that issued it; responses route back to the originating window only.
  • Every other OS call runs in a bounded worker pool. Saturation rejects new work instead of creating unbounded threads or blocking the UI thread.
  • Native long-lived resources (terminal PTYs and WebSocket sessions) survive across calls and keep handles in Rust registries.
  • Bun resources such as SQL connections and backend-only bunWorkers keep their registries in the trusted backend process. Renderer workers remain sandboxed Web Workers and never send executable code over IPC.

Multi-MB payloads skip JSON entirely. A second custom scheme, tynd-bin://localhost/<api>/<method>?<query>, carries raw bytes in the request and response bodies — no base64, no JSON envelope, ArrayBuffer on arrival.

Current routes:

Route Method In Out
fs/readBinary?path=... GET — file bytes
fs/writeBinary?path=...&createDirs=0|1 POST bytes 204
compute/hash?algo=blake3|sha256|sha384|sha512&encoding=base64 POST bytes UTF-8 digest

The origin of the scheme depends on the OS: tynd-bin://localhost on macOS and Linux, http://tynd-bin.localhost on Windows (WebView2 cannot navigate to custom schemes). The host injects the exact origin as window.__TYND_BIN_ORIGIN__ into every page; the client reads it first and only falls back to guessing from the user agent when it is absent, since an app can override navigator.userAgent.

The TS client wraps these — fs.readBinary(path), compute.hash(bytes) — users never touch the scheme directly. Small / non-binary calls (randomBytes, text helpers, terminal events) stay on the JSON IPC where it’s simpler.

See the Binary Data guide.

Any API that pushes async events to the frontend goes through a central emit helper in the Rust host. At startup, the app wires an emitter that turns each emit into a native user event, which the event loop then forwards to the WebView via evaluate_script.

Subscribe on the frontend via window.__tynd__.os_on(name, handler) — or use the typed wrappers (tyndWindow.onResized, a terminal session’s onData, the onProgress option of http requests, …).

  • The primary window has label "main". Additional windows created via multiwindow.create({ label }) (@tynd/plugin-multiwindow) get their own WebView + IPC channel.
  • Every frontend call includes the originating window’s label; responses route back to it only.
  • Window events (resize, focus, close, …) are delivered to every app-local webview (never to remote content) with a label field in the payload. Client-side helpers filter by __TYND_WINDOW_LABEL__ (injected at WebView creation) so handlers only fire for their own window.

See the Multi-Window guide.