Backend RPC
Tynd’s RPC is zero-codegen and zero-schema. Every export from your backend file is a callable RPC method on the frontend, typed via typeof backend.
Core pattern
Section titled “Core pattern”import { app, createEmitter } from "@tynd/core";
// Named exports are RPC methodsexport async function greet(name: string): Promise<string> { return `Hello, ${name}!`;}
export async function add(a: number, b: number): Promise<number> { return a + b;}
// Emitters are frontend event sourcesexport const events = createEmitter<{ tick: { at: number };}>();
app.start({ window: { title: "RPC demo", width: 900, height: 600 },});import { createBackend } from "@tynd/core/client";import type * as backend from "../backend/main";
const api = createBackend<typeof backend>();
const msg = await api.greet("Alice"); // string, typedconst sum = await api.add(2, 3); // number, typed
const unsub = api.on("tick", ({ at }) => { console.log("tick at", at);});// unsub() when doneUnder the hood createBackend is a thin Proxy that sends the call over window.ipc.postMessage. The IPC frame is JSON; arguments, return values, yielded values and event payloads inside it are encoded with devalue, so richer types survive the round trip. No codegen, no IDL — types flow from typeof backend.
Supported argument and return types
Section titled “Supported argument and return types”- Primitives:
string,number,boolean,null,undefined,bigint - Arrays, plain objects, nested structures, cyclic references
Date,Map,Set,RegExp,URL,URLSearchParamsArrayBuffer, typed arrays (Uint8Array, …) andDataView— encoded as base64 text inside the frame (about 4/3 of the size, plus JSON escaping). A frame is capped at 8 MiB, so keep binary payloads under about 5 MiB per call and use binary IPC for anything largerChannel<T>arguments, which the backend pushes messages to (see below)
Not serializable: functions, Error objects and class instances (non-plain objects). Encoding throws; convert them to plain data first.
Error handling
Section titled “Error handling”Throwing in the backend rejects the frontend promise with a TyndIpcError:
// backendexport async function divide(a: number, b: number) { if (b === 0) throw new Error("division by zero"); return a / b;}
// frontendimport { TyndIpcError } from "@tynd/core/client";
try { await api.divide(10, 0);} catch (err) { if (err instanceof TyndIpcError && err.code === "HANDLER_THREW") { console.error(err.message); // "division by zero" }}Only the message crosses IPC: the stack trace and the original error class or name are dropped, and the frontend error has name: "TyndIpcError", a code (HANDLER_THREW for a throw in your handler, other codes for unknown functions, timeouts, permission denials, …) and fn, the RPC function name. For structured business errors, return them as data instead of throwing:
export async function findWidget(id: string) { const widget = await loadWidget(id); if (!widget) return { ok: false as const, code: "not_found" }; return { ok: true as const, widget };}Lifecycle hooks
Section titled “Lifecycle hooks”import { app } from "@tynd/core";
app.onReady(() => { console.log("window shown, WebView alive");});
app.onClose(() => { // 2-second watchdog — if handlers don't complete in time, the host force-exits. console.log("user clicked X, cleaning up");});
app.start({ window: { title: "…" } });onReadyfires when the WebView emitsDOMContentLoadedon the primary window.onClosefires when the user closes the primary window (WindowEvent::CloseRequested). The window hides immediately; you have ~2 s to finish before a watchdog force-exits.
Emitters — typed events
Section titled “Emitters — typed events”export const events = createEmitter<{ userCreated: { id: string; name: string }; progress: { percent: number };}>();
// anywhere in backend codeevents.emit("userCreated", { id: "1", name: "Alice" });events.emit("progress", { percent: 42 });Frontend subscribes:
api.on("userCreated", (user) => console.log(user.name));api.once("userCreated", (user) => { /* fires once */ });Async generators — streaming
Section titled “Async generators — streaming”An async function* export is a streaming RPC:
export async function* count(to: number) { for (let i = 1; i <= to; i++) { yield i; await Bun.sleep(100); } return "done";}
// frontendconst stream = api.count(5);for await (const n of stream) console.log(n); // 1, 2, 3, 4, 5console.log(await stream); // "done"See Streaming RPC for flow control, cancellation, and multi-window routing.
Channels — push to the caller
Section titled “Channels — push to the caller”When the backend should push progress while an ordinary call runs, take a Channel<T> argument. Messages go to the calling webview only, in order.
// backendimport type { Channel } from "@tynd/core";
export async function importFiles(paths: string[], progress: Channel<{ done: number; total: number }>) { for (const [i, path] of paths.entries()) { await importOne(path); progress.send({ done: i + 1, total: paths.length }); } progress.close(); return paths.length;}
// frontendimport { Channel } from "@tynd/core/client";
const progress = new Channel<{ done: number; total: number }>(({ done, total }) => { bar.value = done / total;});const count = await api.importFiles(paths, progress);Use a channel for push-style progress next to a normal result, and an async generator when the frontend pulls a sequence with back-pressure. Details: Channel<T>.
Cancellation and timeouts
Section titled “Cancellation and timeouts”// frontendconst ac = new AbortController();cancelButton.onclick = () => ac.abort();const hits = await api.withOptions({ signal: ac.signal, timeout: 10_000 }).search(query);
// backendimport { getCallContext } from "@tynd/core";
export async function search(query: string) { const { signal } = getCallContext()!; return (await fetch(searchUrl(query), { signal })).json();}Aborting rejects the call with signal.reason (an AbortError); a timeout rejects with a TyndIpcError of code TIMEOUT. Either way, and also when the caller’s page navigates or its window closes, the backend’s signal aborts and the handler’s result is dropped. See Cancellation.
Calling between backend functions
Section titled “Calling between backend functions”Just regular function calls:
async function loadFromDB(id: string) { /* internal */ }
export async function getUser(id: string) { return loadFromDB(id);}Unexported functions are not RPC-accessible — the frontend type import can’t see them.
Modularization
Section titled “Modularization”export { greet, farewell } from "./user";export { list, create } from "./items";export { app } from "@tynd/core";
import { app } from "@tynd/core";app.start({ window: { title: "…" } });export async function greet(name: string) { return `Hi, ${name}`; }export async function farewell(name: string) { return `Bye, ${name}`; }Frontend:
api.greet("Alice");api.list();The typed proxy flattens all re-exports.
Rules of engagement
Section titled “Rules of engagement”One backend file per app
Section titled “One backend file per app”tynd.config.ts::backend points at one entry (default backend/main.ts). All RPC methods and emitters live in the exports of that module (directly or re-exported). This is what types typeof backend — nothing else is reachable.
Never throw sensitive strings
Section titled “Never throw sensitive strings”Error messages cross IPC verbatim. Don’t put DB connection strings, secrets, or file paths you wouldn’t show the user in a thrown message.
Keep RPC granularity moderate
Section titled “Keep RPC granularity moderate”10 calls for one user action cost 10 round-trips + 10 promise resolutions. Batch when you control both sides: expose a single loadDashboard() RPC that returns everything the view needs, not 12 small getters.
Return POJOs, not class instances
Section titled “Return POJOs, not class instances”Class instances cannot be encoded: the call fails. If you use classes internally, convert to plain objects at the RPC boundary.
Bun backend patterns
Section titled “Bun backend patterns”The backend has Bun + Node-compatible + npm access:
// Bun backendimport { readFile } from "node:fs/promises";
export async function readConfig() { return JSON.parse(await readFile("./config.json", "utf8"));}Official plugin clients (@tynd/plugin-*/client) call the host through the WebView bridge, so they run in frontend code:
// frontendimport { os, path } from "@tynd/core/client";import { fs } from "@tynd/plugin-fs/client";
const file = path.join(await os.appConfigDir(), "config.json");const config = JSON.parse(await fs.readText(file));fs belongs to @tynd/plugin-fs; install it with tynd add fs. When backend code needs a native host API, call it with hostCall from @tynd/core.