What the browser sees
A browser page never runs your flow. The flow runs on the server, and the page reaches it through the client, a small typed window with three openings. The page can call an action, follow a session as it happens, and read the data the flow chooses to expose. Client data holds only the exposed fields, but the stream a page follows also carries each change to session state while a request runs, so keep anything a browser must never see out of state.
A flow is a self-contained system on the server, with actions you call and state of its own. A session is the record it keeps of many requests together.
The example on this page is a support desk: a flow called support with an action called reply.
Wrapping the app
FlowProvider tells every hook below it which flow to talk to, who the user is, and where the server answers. Its props are in the Client reference.
import { FlowProvider } from "@flow-state-dev/react";
import { SupportDesk } from "./support-desk";
export function SupportPage({ userId }: { userId: string }) {
return (
<FlowProvider flowKind="support" userId={userId} baseUrl="">
<SupportDesk />
</FlowProvider>
);
}
Calling an action
import { useAction } from "@flow-state-dev/react";
export function ReplyBox({ sessionId }: { sessionId: string }) {
const reply = useAction({ action: "reply" });
return (
<button
disabled={reply.loading}
onClick={() => void reply.execute({ text: "Where is order A-1001?" }, sessionId)}
>
Send
</button>
);
}
execute resolves when the server accepts the request, usually before the work is done. The answer arrives through the session. The Client reference covers the options.
Following a session
import { ItemsRenderer, useSession } from "@flow-state-dev/react";
export function Conversation({ sessionId }: { sessionId: string }) {
const session = useSession(sessionId);
return <ItemsRenderer items={session.items} />;
}
useSession returns the session's items: messages, tool calls, status cards. While a request streams, the text of a message grows token by token. By default the hook follows only the requests it sent itself, with session.sendAction("reply", { text }). A request sent through useAction, as ReplyBox does, is not one of them. Send it with sendAction to watch it stream, or pass useSession(sessionId, { live: true }): the hook then follows every request in the session, another agent's tab included, and each finished item joins items whole, a moment after it is saved.
Sessions, items and streaming covers items and the stream.
Reading what the flow exposes
Session state reaches client data only when the flow names a field in client.expose.
import { defineFlow } from "@flow-state-dev/core";
import { z } from "zod";
import { reply } from "./blocks/reply";
export default defineFlow({
kind: "support",
actions: { reply },
session: {
stateSchema: z.object({
ticketStatus: z.enum(["open", "waiting", "solved"]).default("open"),
internalNotes: z.array(z.string()).default([]),
}),
client: { expose: ["ticketStatus"] },
},
})();
reply is a block, the framework's unit of work, in its own file beside the flow. On the page:
import { useClientData, useSession } from "@flow-state-dev/react";
export function TicketBadge({ sessionId }: { sessionId: string }) {
const session = useSession(sessionId);
const data = useClientData(session, { session: ["ticketStatus", "internalNotes"] });
return <span>{String(data.session.ticketStatus)}</span>;
}
data.session.ticketStatus is "open", then "solved" as the flow changes it, mid-stream. data.session.internalNotes is always undefined, because the flow never named it.
When the page needs an answer in a different shape, a view computes it on the server. Views covers them.
Rendering items
ItemsRenderer draws a list of items in order. It has a built-in for messages, reasoning, status, errors, tool output and suspension cards. A data item draws nothing until a renderer is registered for it, and any other type draws a placeholder, not the item's content. The UI components reference lists what it draws.
To draw a type yourself, register a renderer on FlowProvider. A data item is a structured record a flow emits under a name, its dataType, and the data renderer map is keyed by that name:
import type { DataItem } from "@flow-state-dev/core/items";
export function OrderCard({ item }: { item: DataItem }) {
return <div>Order {String(item.value.orderId)}</div>;
}
In SupportPage, add renderers={{ data: { "order-card": OrderCard } }} to FlowProvider.
What the browser never gets
A field in session state that is not in client.expose never appears in useClientData or the session snapshot. It is not a secret, though: while a request runs, a change to it can travel in the stream's state-change events, so keep secrets out of session state. Items the flow marks as not for the client, such as block traces and router decisions, are removed from the stream. The userId the page sends is caller input, not proof. When the server checks who is calling (see the Client reference), it ignores this userId and uses the request's credentials.
Without React
Hooks are a thin layer. Outside React, import createClient from @flow-state-dev/client. It has no React or DOM dependency and runs in Node, the browser and edge runtimes.
import { createClient } from "@flow-state-dev/client";
import type supportFlow from "../flows/support/flow";
export async function askSupport(userId: string, text: string) {
const fsd = createClient({ userId, baseUrl: "https://support.example.com" });
const support = fsd.flow<typeof supportFlow>("support");
const session = await support.createSession({ title: "Billing question" });
const sent = await support.send("reply", { text }, { sessionId: session.id });
const final = await sent.wait();
return final.status;
}
sent.stream({ ... }) follows that one request as it runs. It does not reconnect on its own: if the connection drops, open it again with startingAfter set to the last sequence number you saw, and the server sends what you missed before carrying on. fsd.sessions.stream(sessionId) follows the whole session and retries by itself.
import type keeps the flow's server code out of a browser bundle, and the type is what makes a misspelt action name a compile error. Client options has the rest.
What it gets you
- Types follow the flow.
replywith the wrong input fails to compile. - A new field stays out of client data. Add
refundLimitto session state anduseClientDatareturns nothing for it until you name it inexpose. - A dropped stream loses nothing. Reopen a request's stream with
startingAfterand the server sends what you missed;useSessionand a session stream reconnect by themselves. - Credentials live in one place. Pass a
fetcherthat adds anAuthorizationheader toFlowProvider(orcreateClient), and every call and stream carries it.
How it fits with the others
The client is the page's half of Flows and actions: it names a flow and an action and sends input. What it reads is decided by State and scopes. Hooks for resources, such as useResourceCollection, are listed in the Client reference.
Next
- Sessions, items and streaming: the items the page follows and how they arrive.
- State and scopes: what
exposecopies and from which scope. - Client reference: every
createClientandFlowProvideroption. - UI components: ready-made renderers.