Concepts
IPC
Commands, events and streams between your Rust code and your page.
Your page runs in a renderer process. Your Rust code runs in the browser process. IPC is the only path between them. A page reaches only what your app registers on that path.
Three ways to talk
| Kind | Started by | Shape |
|---|---|---|
| Command | The page | One request and one reply |
| Event | Your app | One message to every page subscribed to it |
| Stream | The page | Chunks in both directions until one side ends it |
JavaScript values travel as JSON. Bytes travel as they are. A frame’s messages arrive in the order they were sent.
Commands
A command is a named Rust handler. The page calls it with window.kurogane.invoke and gets a promise back. The promise settles with the handler’s reply or its error.
use kurogane::{App, AppHandle};
use serde_json::{Value, json};
App::new("dist")
.command("ping", |payload: Value, _: &AppHandle| Ok(json!({ "echo": payload })))
.run_or_exit();
const reply = await window.kurogane.invoke("ping", { message: "hello" });
Four registrations cover the shapes a command takes:
App::commandanswers at once.App::async_commandanswers later through aResponder. Any thread may resolve it.App::binary_commandtakes and returns raw bytes without JSON.App::async_binary_commandis the async form of a binary command.
Every call settles. A responder dropped unanswered rejects its call. A page that cancels its call, navigates away or closes marks the responder cancelled; a cancelled responder sends nothing.
Events
An event is a named message your app sends to every page subscribed to it. AppHandle::broadcast_json sends JSON. AppHandle::broadcast sends bytes.
app.broadcast_json("progress", &42);
A page subscribes with kurogane.on and stops with kurogane.off:
const id = window.kurogane.on("progress", (json) => update(JSON.parse(json)));
window.kurogane.off(id);
Streams
A stream carries chunks in both directions between one page and one StreamHandler. App::stream registers a factory that makes a handler for each stream a page opens.
A stream moves through four steps:
- The handler accepts or refuses the stream before the page holds it.
- The page’s
openStreamresolves. Both sides may send from here on. - The handler receives each chunk the page writes.
- The page ends the stream or either side fails it.
The first end or error sent closes the stream. Later sends fail. The page always hears back when it ends a stream. A StreamResponder sends to the page and works from any thread.
Who may call
Kurogane checks the calling page’s origin before any handler runs. By default only your app’s own origin passes. That’s app://app for App::new and the start URL’s origin for App::url. A page of any other origin is refused. That covers popups, iframes and windows that followed a link.
App::permitopens a command or stream to the origins you name.App::permit_eventdoes the same for event subscriptions.App::deny_unlistedcloses every name without a rule, your own pages included.
A sandboxed frame has an opaque origin and matches no rule.
Errors
A handler fails with an IpcError. A string converts into one. The page’s promise rejects with an Error whose code names what failed. Kurogane’s own codes are zero or negative. Your app may send positive codes of its own. ErrorCode lists every code.
Threads
Handlers run on the UI thread. That thread also runs every window and receives every message from every page. Return promptly and move slow work to a thread of your own. A Responder and a StreamResponder work from any thread.
Size
One message from a page has a size cap. A call over it rejects with ErrorCode::TooLarge before anything is sent. Stream larger data in chunks.
Pages that come back
A page restored from the back-forward cache finds the calls it left open rejected with ErrorCode::Left. Its open streams fail. It keeps its event subscriptions but misses the events sent while it was away.