Axum
Expose procedures over HTTP and WebSockets.
The experimental Axum adapter mounts built procedures in an Axum router. Use the dependencies from the quickstart, which pairs this adapter with Axum 0.8.
Mount an endpoint
let (procedures, types) = router().build().expect("invalid router");
rspc::Typescript::default().export_to("./bindings.ts", &types)?;
let app = axum::Router::new()
.nest("/rspc", rspc_axum::endpoint(procedures, || ()));The context callback above returns (). The quickstart includes binding a listener and serving the router.
Extract request context
The adapter accepts context callbacks with Axum request-parts extractors. For example, read headers from Parts:
use axum::http::request::Parts;
#[derive(Clone)]
struct Ctx {
authorization: Option<String>,
}
let app = axum::Router::new().nest(
"/rspc",
rspc_axum::endpoint(procedures, |parts: Parts| Ctx {
authorization: parts.headers
.get("authorization")
.and_then(|value| value.to_str().ok())
.map(str::to_owned),
}),
);Here procedures must have been built from a Router<Ctx>. Reading a header does not authenticate the caller. Verify the credential before allowing access to protected data.
Only request parts are available to this callback. The adapter consumes the body to decode the procedure input.
HTTP client
import { createClient, FetchTransport } from "@rspc/client";
import type { ProceduresLegacy } from "./bindings";
const client = createClient<ProceduresLegacy>({
transport: new FetchTransport("/rspc"),
});
const version = await client.query(["version", null]);The tuple client uses ProceduresLegacy from rspc's exporter. HTTP fetch supports queries and mutations, not subscriptions.
WebSockets
Enable the adapter's ws feature in Cargo.toml. The endpoint then accepts upgrades at /rspc/ws:
import { createClient, WebsocketTransport } from "@rspc/client";
import type { ProceduresLegacy } from "./bindings";
const client = createClient<ProceduresLegacy>({
transport: new WebsocketTransport("ws://localhost:4000/rspc/ws"),
});Use wss:// when the site is served over HTTPS. Configure your proxy to forward WebSocket upgrades. The callback receives request parts from the original upgrade request, so do not assume its headers change during the connection.
Deployment
For a browser on another origin, configure CORS in Axum. Credentialed requests need an explicit allowed origin and credentials support; a wildcard origin is not enough. Prefer same-origin deployment when possible.
Set request limits and authentication policy at your server or reverse proxy. This adapter is experimental and should not be treated as a hardened public gateway.
Last updated on
