The RivetKit Rust client provides a way to connect to and interact with actors from Rust applications.
Quickstart
Create a new Rust project
Create a new Rust project:
cargo new my-app
cd my-app
Add dependencies
Add RivetKit client & related dependencies to your project:
cargo add rivetkit-client
cargo add serde_json
cargo add tokio --features full
Create your client
Modify src/main.rs
to connect to your actor:
use rivetkit_client::{Client, EncodingKind, GetOrCreateOptions, TransportKind};
use serde_json::json;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Replace with your endpoint URL after deployment
let client = Client::new(
"http://localhost:8080",
TransportKind::Sse,
EncodingKind::Json
);
// Get or create an actor instance
let options = GetOrCreateOptions::default();
let counter = client.get("counter", [].into(), options)?
.connect();
// Subscribe to events
counter.on_event("newCount", |args| {
let count = args[0].as_i64().unwrap();
println!("Event: {}", count);
}).await;
// Call an action
let result = counter.action("increment", vec![json!(5)]).await?;
println!("Action: {}", result);
// Wait to receive events
tokio::time::sleep(Duration::from_secs(1)).await;
Ok(())
}
Run your client
In a separate terminal, run your client code:
You should see output like:
Run it again to see the state update.