SwiftUI
Build SwiftUI apps with Rivet Actors.
Install
Add the Swift package dependency and import RivetKitSwiftUI:
RivetKitSwiftUI re-exports RivetKitClient and SwiftUI, so a single import covers both.
Minimal Client
Actor Options
The @Actor property wrapper always uses get-or-create semantics and accepts:
name(required)keyasStringor[String](required)params(optional connection parameters)createWithInput(optional creation input)createInRegion(optional creation hint)enabled(toggle connection lifecycle)
Actions
Subscribing to Events
Async Event Streams
Connection Status
Error Handling
Concepts
Keys
Keys uniquely identify actor instances. Use compound keys (arrays) for hierarchical addressing:
Don’t build keys with string interpolation like "org:\(userId)" when userId contains user data. Use arrays instead to prevent key injection attacks.
Environment Configuration
Call .rivetKit(endpoint:) or .rivetKit(client:) once at the root of your view tree:
When using .rivetKit(endpoint:), the client is created once and cached per endpoint. When using .rivetKit(client:), store the client as a property on App (not inside body) since SwiftUI can call body multiple times.
Environment Variables
ClientConfig reads optional values from environment variables:
RIVET_NAMESPACE- Namespace (can also be in endpoint URL)RIVET_TOKEN- Authentication token (can also be in endpoint URL)RIVET_RUNNER- Runner name (defaults to"default")
The endpoint is always required. There is no default endpoint.
Endpoint Format
Endpoints support URL auth syntax:
You can also pass the endpoint without auth and provide RIVET_NAMESPACE and RIVET_TOKEN separately. For serverless deployments, set the endpoint to your app’s /api/rivet URL. See Endpoints for details.
API Reference
Property Wrapper
@Actor(name, key:, params:, createWithInput:, createInRegion:, enabled:)- SwiftUI property wrapper for actor connections
View Modifiers
.rivetKit(endpoint:)- Configure client with an endpoint URL (creates cached client).rivetKit(client:)- Configure client with a custom instance.onActorEvent(actor, event) { ... }- Subscribe to actor events (supports 0–5 typed args).onActorError(actor) { error in ... }- Handle actor errors
ActorObservable
actor.action(name, args..., as:)- Async action callactor.send(name, args...)- Fire-and-forget actionactor.events(name, as:)- AsyncStream of typed eventsactor.connStatus- Current connection statusactor.isConnected- Whether connectedactor.handle- UnderlyingActorHandle(optional)actor.connection- UnderlyingActorConnection(optional)actor.error- Most recent error (optional)
Types
ActorConnStatus- Connection status enum (.idle,.connecting,.connected,.disconnected,.disposed)ActorError- Typed actor errors withgroup,code,message,metadata