Connect a client
@rxferrix/client handles subscriptions, reconnects, and requests. @rxferrix/react and @rxferrix/svelte add framework bindings. All use the function references generated from your backend.
The examples use a tasks module with list, add, and set_done functions. The functions guide defines those functions; the project generator includes a simpler unfiltered list.
Set up React
Install @rxferrix/react and create one client outside component rendering:
import { createRoot } from "react-dom/client";
import { RxFerrixClient, RxFerrixProvider } from "@rxferrix/react";
import { App } from "./App";
const client = new RxFerrixClient(window.location.origin);
createRoot(document.getElementById("root")!).render(
<RxFerrixProvider client={client}>
<App />
</RxFerrixProvider>,
);The generated app already does this. window.location.origin works when Vite proxies /api during development and when the backend serves the built frontend. For a separate backend origin, pass that URL and configure the server's CORS origins.
The provider makes the client available to descendant hooks. If you own the client lifetime, call client.close() when the app no longer needs it.
Read data in React
import { useQuery, useQueryWithStatus } from "@rxferrix/react";
import { api } from "./rxferrix";
const tasks = useQuery(api.tasks.list, { done: false });
const { data, error, isLoading } = useQueryWithStatus(api.tasks.list, {});Use one of these hooks for a given view. useQuery returns undefined while loading and throws a query error for an error boundary. useQueryWithStatus returns the error as a value for inline rendering.
Arguments are compared by value, so a fresh inline object does not resubscribe. Pass "skip" as the arguments when required inputs are unavailable. Changing arguments normally shows loading until the new result arrives. useQueryKeepingPrevious retains the previous result and marks it with isStale while the next result loads.
Set up Svelte 5
Install @rxferrix/svelte. Call setupRxFerrix during component initialization, usually in the root component:
<script lang="ts">
import { setupRxFerrix } from "@rxferrix/svelte";
setupRxFerrix(window.location.origin);
</script>A URL creates a client that closes when the component is destroyed. Passing an existing client leaves its lifetime to you. This browser setup uses window; a server-rendered application needs to manage client creation for its own lifecycle.
In a component using the client:
<script lang="ts">
import { useQuery } from "@rxferrix/svelte";
import { api } from "./rxferrix";
let done = $state(false);
const tasks = useQuery(api.tasks.list, () => ({ done }));
</script>
{#if tasks.isLoading}
<p>Loading…</p>
{:else if tasks.error}
<p>{tasks.error.message}</p>
{:else}
<ul>
{#each tasks.data ?? [] as task (task._id)}
<li>{task.text}</li>
{/each}
</ul>
{/if}Svelte arguments are a function so reactive inputs are tracked. Return "skip" to disable the subscription. Read tasks.data, tasks.error, tasks.isLoading, and tasks.isStale directly in reactive code; destructuring their current values loses that reactive access. Use { keepPreviousData: true } as the third argument to retain previous results while arguments change.
Call mutations and actions
React and Svelte expose useMutation and useAction:
const add = useMutation(api.tasks.add);
await add({ text: "Buy milk" });Call the hook where your framework expects it, then invoke the returned function from an event handler. Catch rejected promises to display failures. A mutation promise resolves after the client's subscribed results reach the mutation's commit. Actions resolve when their work finishes.
Subscriptions update automatically after a mutation. You do not call a refetch method. Results are read-only: use a copy before sorting or changing an array.
Add an optimistic update
Show a change before the server confirms it by updating matching local query results. For example, inside a React component:
const setDone = useMutation(api.tasks.set_done).withOptimisticUpdate((store, args) => {
const current = store.getQuery(api.tasks.list, {});
if (current !== undefined) {
store.setQuery(api.tasks.list, {}, current.map((task) =>
task._id === args.id ? { ...task, done: args.done } : task
));
}
});This updates the unfiltered list subscription with {} arguments. If you show filtered lists too, update those matching cached queries with the appropriate membership rules.
The client replays optimistic updates over every server result until the mutation is visible or fails. Build new arrays and objects. For optimistic inserts, generate temporary IDs and timestamps once per invocation, outside the replayed closure.
Paginate a list
For the page query in the functions guide, a React component can use:
const { results, status, loadMore } = usePaginatedQuery(
api.tasks.page,
{},
{ initialNumItems: 20 },
);
// In the component's returned JSX:
<button disabled={status !== "CanLoadMore"} onClick={() => loadMore(20)}>
Load more
</button>Import usePaginatedQuery from your binding. React also has usePaginatedQueryWithStatus for inline page errors. Svelte's hook returns a reactive object with results, status, error, and loadMore; pass its arguments as a function and read its fields directly.
Loaded pages remain live. insertAtTop and getAllPages support optimistic changes to paginated results; the chat example uses both.
Use the core client
Without React or Svelte, install @rxferrix/client. It also runs outside a browser, in a script or a Node service: it needs a global fetch and WebSocket, which Node 22 and later provide. On older versions, pass webSocketConstructor (for example from the ws package) in the client's options. The generated API is TypeScript with extensionless imports (./rxferrix is a folder): run scripts with Bun or tsx, or bundle them, rather than with plain node.
import { RxFerrixClient } from "@rxferrix/client";
import { api } from "./rxferrix";
const client = new RxFerrixClient("http://127.0.0.1:8090");
const watch = client.watchQuery(api.tasks.list, {});
const unsubscribe = watch.onUpdate(() => {
const state = watch.localQueryState();
if (state.status === "success") console.log(state.value);
if (state.status === "error") console.error(state.error.message);
});
await client.mutation(api.tasks.add, { text: "Buy milk" });
const tasks = await client.query(api.tasks.list, {});
unsubscribe();
client.close();watchQuery follows a query: it subscribes when you register a callback with onUpdate and stays subscribed while one is registered. localQueryState() never throws. It returns { status: "loading" }, { status: "success", value }, or { status: "error", error }, where error is an RxFerrixError. localQueryResult() returns the value, undefined while loading, or throws the error. query is for a one-off read: it waits for the first result, unsubscribes, and resolves with the value or rejects with the error.
A paginated query has a different shape. client.paginatedQuery(api.tasks.page, {}, { initialNumItems: 20 }) returns an object with subscribe(callback), which returns the unsubscribe function, and getCurrent(), which returns { results, status, isLoading, error, loadMore }. Pages stay subscribed while a callback is registered.
The core client uses a WebSocket even for one-shot query calls. For direct HTTP requests, use the HTTP API reference.
Sign in from a script
getAuth(client) keeps tokens in localStorage where one is available (Node can provide one when configured to), otherwise in memory; storage: new MemoryAuthStorage() keeps them in memory either way. To choose, pass options on the first call; later calls return the same instance and ignore them:
import { getAuth, MemoryAuthStorage } from "@rxferrix/client";
const auth = getAuth(client, { storage: new MemoryAuthStorage() });
await auth.signIn({ email: "ann@example.com", password: "correct horse battery" });signUp({ email, password, name? }) creates a password account and resolves { verificationRequired }. For an email code, requestMagicLink({ email }) sends one (in --dev without SMTP it appears in the backend log), and verifyCode({ email, code }) signs in. signIn and verifyCode resolve { mfaRequired: false }, or { mfaRequired: true, mfaToken } for an account with two-factor authentication. Once signed in, the client's calls run as that user.
Upload a file from a script
uploadFile takes any Blob. In --dev, upload URLs are root-relative (/api/storage/upload?...) unless the server has a --public-url. A browser resolves them against the page; elsewhere, pass the server's address as baseUrl:
import { readFile } from "node:fs/promises";
import { uploadFile } from "@rxferrix/client";
const url = await client.mutation(api.files.generate_upload_url, {});
const blob = new Blob([await readFile("photo.png")], { type: "image/png" });
const storageId = await uploadFile(url, blob, { baseUrl: "http://127.0.0.1:8090", filename: "photo.png" });A Blob has no name, so pass filename to store one. Without XMLHttpRequest, the upload uses fetch, and onProgress reports only the start and the end.
Use the client in another project
The client packages are on npm: @rxferrix/client, @rxferrix/react, and @rxferrix/svelte. The generated API files import from @rxferrix/client, so add it to any project that uses them. From the backend's directory, write the generated files into that project:
cargo run -- codegen --out ../my-script/src/rxferrixTo regenerate on every development start instead, run cargo run -- serve --dev --codegen-out DIR, or set RXFERRIX_CODEGEN_OUT=DIR. Without either, serve --dev writes no files. A web app made by rxferrix new sets RXFERRIX_CODEGEN_OUT in its .cargo/config.toml, which cargo run and rxferrix dev apply; a binary started directly does not read that file. codegen without --out also uses RXFERRIX_CODEGEN_OUT, else rxferrix-generated.
Auth, connection state, and retries
React has useRxFerrixAuth, useAuthActions, and useCurrentUser; Svelte has useAuth and useCurrentUser. See Auth for setup and external token providers.
useConnectionState() returns connection/request state in React. Svelte returns an object whose .current is reactive. Use this to show connection loss or pending work. With the core client, client.connectionState() returns the current state, and client.subscribeToConnectionState(callback) calls back with each change and returns the unsubscribe function. The state has isWebSocketConnected, hasEverConnected, connectionCount, connectionRetries, hasInflightRequests, inflightMutations, inflightActions, timeOfOldestInflightRequest (a Date or null), and isAuthenticated (the server accepted the current token).
Request failures are RxFerrixError objects. Expected server errors preserve the UserError code, message, and data. Client errors include ConnectionLost, ClientClosed, OutcomeUnknown, too_many_queries, and args_too_large (arguments that do not fit in one 2 MiB message; the call is refused before anything is sent).
Unanswered mutations are retried only within the supported window and identity. An OutcomeUnknown means the mutation may already have committed. Actions interrupted by connection loss reject with ConnectionLost and are not automatically retried. See reconnect behavior before adding application retries.