# Start

> Read when starting with Sorus. Install it, make a realm, boxes, and a computed value for a chat's composer, write in an update, and show the value in React.

## Install

Each package has the same name under every package manager.

```bash
bun add @frondruntime/sorus
# or npm install, pnpm add, yarn add
```

`@frondruntime/sorus-react` adds React hooks, with React 19 or later as a peer
dependency. `@frondruntime/sorus-effect` adds Effect Schema, with `effect` 4.1
or a later 4.x as a peer dependency; `sorus` and `sorus-react` never import
it, so an application without Effect never installs it.

```bash
bun add @frondruntime/sorus @frondruntime/sorus-react
bun add @frondruntime/sorus @frondruntime/sorus-effect effect
```

The rules for an agent install as skills, one per task:
[Agent skills](./skills.md).

## Example

The samples on these pages build one chat: rooms, messages that stream in
over a socket, a composer, and a message that shows as pending until the
server takes it. Its boxes hold the open room, the composer's fields, and the
socket's state.

```ts
import { Box } from "@frondruntime/sorus";
import { app } from "./realm.ts";

interface Composer {
  readonly text: string;
  readonly replyTo: string | null;
}

const $roomId = Box.make(app, "general", { name: "roomId" });
const $composer = Box.make<Composer>(app, { text: "", replyTo: null }, { name: "composer" });
const $connection = Box.make<"connecting" | "open" | "closed">(app, "connecting", {
  name: "connection",
});
```

An update writes the boxes; the throw undoes the first write.

```ts
import { Realm } from "@frondruntime/sorus";
import { app } from "./realm.ts";

const typeText = (text: string) => {
  Realm.update(app, (read, write) => {
    write($composer, { ...read($composer), text });
  });
};

const openRoom = (roomId: string) => {
  Realm.update(app, (_read, write) => {
    write($composer, { text: "", replyTo: null });
    // Throwing undoes the write above: the composer keeps its text.
    if (roomId === "") throw new Error("a room id is not blank");
    write($roomId, roomId);
  });
};
```

## Rules

- Name an observable with a leading `$`. Everything else is plain data, so a
  read of an observable is visible in the code.
- Read and write through the place that hands them out: `read` and `write`
  are not methods of an observable, so a misplaced one throws where it is made.
- Pass the realm to a call that makes something or opens an update, and the
  observable to a call about one: there is no default realm to fall back on.
- Import from `@frondruntime/sorus` and `@frondruntime/sorus-react` for
  Effect-free code, and from `@frondruntime/sorus-effect`, which re-exports
  every call of `sorus`, where a schema checks writes.

## Answers

| Call | Answers |
| --- | --- |
| `Realm.make()` | The realm that holds every observable it makes: [Realms](../state/realms.md). |
| `Box.make(realm, initial)` | One value, written only in an update: [Boxes and computed](../state/boxes.md). |
| `Computed.make(realm, (read) => ...)` | A cached value derived from what the function reads: [Boxes and computed](../state/boxes.md). |
| `Realm.update(realm, (read, write) => ...)` | Runs the recipe atomically: [Updates](../state/updates.md). |
| `useValue($x)` | The value of `$x`, rerendering when it changes: [Hooks](../react/hooks.md). |

## See also

- [Why Sorus](./why.md): the principles, their costs, and the packages.
- [Coming from MobX](./mobx.md): the Sorus call for each MobX one.
- [Realms](../state/realms.md): where state lives and when it ends.
- [Boxes and computed](../state/boxes.md): boxes, computed values, atoms, models.
- [Schema-checked boxes](../effect/boxes.md): the same calls on Effect Schema.
