Swift
Connect Swift apps to Rivet Actors.
Install
Add the Swift package dependency and import RivetKitClient:
Minimal Client
Stateless vs Stateful
Getting Actors
Actions support positional overloads for 0–5 args:
If you need more than 5 arguments, use the raw JSON fallback:
Connection Parameters
Subscribing to Events
Event streams support 0–5 typed arguments. If you need raw values or more than 5 arguments, use JSONValue:
Connection Lifecycle
Low-Level HTTP & WebSocket
For actors that implement onRequest or onWebSocket, you can call them directly:
Calling from Backend
Use the same client in server-side Swift (Vapor, Hummingbird, etc.):
Error Handling
If you need an untyped response, you can decode to JSONValue:
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 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 parameter 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
Client
RivetKitClient(config:)- Create a client with a configClientConfig- Configure endpoint, namespace, and tokenclient.get()/getOrCreate()/getForId()/create()- Get actor handlesclient.dispose()- Dispose the client and all connections
ActorHandle
handle.action(name, args..., as:)- Stateless action callhandle.connect()- Create a stateful connectionhandle.resolve()- Get the actor IDhandle.getGatewayUrl()- Get the raw gateway URLhandle.fetch(path, request:)- Raw HTTP requesthandle.websocket(path:)- Raw WebSocket connection
ActorConnection
conn.action(name, args..., as:)- Action call over WebSocketconn.events(name, as:)- AsyncStream of typed eventsconn.statusChanges()- AsyncStream of status changesconn.errors()- AsyncStream of connection errorsconn.opens()- AsyncStream that yields on connection openconn.closes()- AsyncStream that yields on connection closeconn.currentStatus- Current connection statusconn.dispose()- Close the connection
Types
ActorConnStatus- Connection status enum (.idle,.connecting,.connected,.disconnected,.disposed)ActorError- Typed actor errors withgroup,code,message,metadataJSONValue- Raw JSON value for untyped responses