# 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.

```rust
use kurogane::{App, AppHandle};
use serde_json::{Value, json};

App::new("dist")
    .command("ping", |payload: Value, _: &AppHandle| Ok(json!({ "echo": payload })))
    .run_or_exit();
```

```javascript
const reply = await window.kurogane.invoke("ping", { message: "hello" });
```

Four registrations cover the shapes a command takes:

* [`App::command`](https://kurogane-rs.org/docs/api/app/app.md#method.command) answers at once.
* [`App::async_command`](https://kurogane-rs.org/docs/api/app/app.md#method.async_command) answers later through a [`Responder`](https://kurogane-rs.org/docs/api/ipc/responder.md). Any thread may resolve it.
* [`App::binary_command`](https://kurogane-rs.org/docs/api/app/app.md#method.binary_command) takes and returns raw bytes without JSON.
* [`App::async_binary_command`](https://kurogane-rs.org/docs/api/app/app.md#method.async_binary_command) is 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`](https://kurogane-rs.org/docs/api/app/apphandle.md#method.broadcast_json) sends JSON. [`AppHandle::broadcast`](https://kurogane-rs.org/docs/api/app/apphandle.md#method.broadcast) sends bytes.

```rust
app.broadcast_json("progress", &42);
```

A page subscribes with `kurogane.on` and stops with `kurogane.off`:

```javascript
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`](https://kurogane-rs.org/docs/api/ipc/streamhandler.md). [`App::stream`](https://kurogane-rs.org/docs/api/app/app.md#method.stream) registers a factory that makes a handler for each stream a page opens.

A stream moves through four steps:

1. The handler accepts or refuses the stream before the page holds it.
2. The page's `openStream` resolves. Both sides may send from here on.
3. The handler receives each chunk the page writes.
4. 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`](https://kurogane-rs.org/docs/api/ipc/streamresponder.md) 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::permit`](https://kurogane-rs.org/docs/api/app/app.md#method.permit) opens a command or stream to the origins you name.
* [`App::permit_event`](https://kurogane-rs.org/docs/api/app/app.md#method.permit_event) does the same for event subscriptions.
* [`App::deny_unlisted`](https://kurogane-rs.org/docs/api/app/app.md#method.deny_unlisted) closes 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`](https://kurogane-rs.org/docs/api/ipc/ipcerror.md). 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`](https://kurogane-rs.org/docs/api/ipc/errorcode.md) 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`](https://kurogane-rs.org/docs/api/ipc/errorcode.md#variant.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`](https://kurogane-rs.org/docs/api/ipc/errorcode.md#variant.Left). Its open streams fail. It keeps its event subscriptions but misses the events sent while it was away.
