This package is a Tauri extension to give you a fully-typed IPC layer for Tauri commands and events.
The TS types corresponding to your pre-defined Rust backend API are generated on runtime, after which they can be used to call the backend from your TypeScript frontend framework of choice. This crate provides typesafe bidirectional IPC communication between the Rust backend and TypeScript frontend. Specta is used under the hood for the type-generation.
First, add the following crates to your Cargo.toml
:
# src-tauri/Cargo.toml
[dependencies]
taurpc = "0.1.6"
specta = { version = "1.0.5", features = ["export"] }
tokio = { version = "1", features = ["full"] }
Then, declare and implement your IPC methods and resolvers. If you want to use your API for Tauri's events, you don't have to implement the resolvers, go to Calling the frontend
// src-tauri/src/main.rs
#[taurpc::procedures]
trait Api {
async fn hello_world();
}
#[derive(Clone)]
struct ApiImpl;
#[taurpc::resolvers]
impl Api for ApiImpl {
async fn hello_world(self) {
println!("Hello world");
}
}
#[tokio::main]
async fn main() {
tauri::Builder::default()
.invoke_handler(taurpc::create_ipc_handler(ApiImpl.into_handler()))
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
The #[taurpc::procedures]
trait will generate everything necessary for handling calls and the type-generation. Now, you should run pnpm tauri dev
to generate and export the TS types.
The types will by default be exported to node_modules/.taurpc
, but you can specify an export path by doing this #[taurpc::procedures(export_to = "../bindings.ts")]
.
Then on the frontend install the taurpc package.
pnpm install taurpc
Now on the frontend you import the generated types, if you specified the export_to
attribute on your procedures you should import your from there. By default you can import from .taurpc
.
With these types a typesafe proxy is generated that you can use to invoke commands and listen for events.
import { createTauRPCProxy } from '.taurpc'
const taurpc = await createTauRPCProxy()
await taurpc.hello_world()
The types for taurpc are generated once you start your application, run pnpm tauri dev
. If the types are not picked up by the LSP, you may have to restart typescript to reload the types.
You can find a complete example (using Svelte) here.
If you want to you structs for the inputs/outputs of procedures, you should always add #[taurpc::ipc_struct]
to make sure the coresponding ts types are generated.
#[taurpc::ipc_struct]
struct User {
user_id: u32,
first_name: String,
last_name: String,
}
#[taurpc::procedures]
trait Api {
async fn get_user() -> User;
}
To share some state between procedures, you can add fields on the API implementation struct. If the state requires to be mutable, you need to use a container that enables interior mutability, like a Mutex.
You can use the window
and app_handle
arguments just like with Tauri's commands. Tauri docs
// src-tauri/src/main.rs
use std::sync::{Arc, Mutex};
use tauri::{Manager, Runtime, State, Window};
type MyState = Arc<Mutex<String>>;
#[taurpc::procedures]
trait Api {
async fn with_state();
async fn with_window<R: Runtime>(window: Window<R>);
}
#[derive(Clone)]
struct ApiImpl {
state: MyState
};
#[taurpc::resolvers]
impl Api for ApiImpl {
async fn with_state(self) {
// ...
// self.state.lock()
// ...
}
async fn with_window<R: Runtime>(self, window: Window<R>) {
// ...
}
}
#[tokio::main]
async fn main() {
tauri::Builder::default()
.invoke_handler(taurpc::create_ipc_handler(
ApiImpl {
state: Arc::new(Mutex::new("state".to_string())),
}
.into_handler(),
))
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
You can return a Result<T, E>
to return an error if the procedure fails. This is will reject the promise on the frontend and throw an error.
If you're working with error types from Rust's std library, they will probably not implement serde::Serialize
which is required for anything that is returned in the procedure.
In simple scenarios you can use map_err
to convert these errors to String
s. For more complex scenarios, you can create your own error type that implements serde::Serialize
.
You can find an example using thiserror here.
You can also find more information about this in the Tauri guides.
Inside your procedures trait you can add attributes to the defined methods. This can be used to ignore or rename a method. Renaming will change the name of the procedure on the frontend.
#[taurpc::procedures]
trait Api {
// #[taurpc(skip)]
#[taurpc(alias = "_hello_world_")]
async fn hello_world();
}
Trigger events on your TypeScript frontend from your Rust backend with a fully-typed experience.
The #[taurpc::procedures]
macro also generates a struct that you can use to trigger the events, this means you can define the event types the same way you define the procedures.
First start by declaring the API structure, by default the event trigger struct will be identified by TauRpc{trait_ident}EventTrigger
. If you want to change this, you can add an attribute to do this, #[taurpc::procedures(event_trigger = ApiEventTrigger)]
.
For more details you can look at the example.
// src-tauri/src/main.rs
#[taurpc::procedures(event_trigger = ApiEventTrigger)]
trait Api {
async fn hello_world();
}
#[tokio::main]
async fn main() {
tauri::Builder::default()
.setup(|app| {
let trigger = ApiEventTrigger::new(app.handle());
trigger.hello_world()?;
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Then, on the frontend you can listen for the events with types:
const unlisten = taurpc.hello_world.on(() => {
console.log('Hello World!')
})
// Run this inside a cleanup function, for example in React and onDestroy in Svelte
unlisten()
By default, events are emitted to all windows. If you want to send an event to a specific window by label, you can do the following:
use taurpc::Windows;
trigger.send_to(Windows::One("main".to_string())).hello_world()?;
// Options:
// - Windows::All (default)
// - Windows::One(String)
// - Windows::N(Vec<String>)
- Basic inputs
- Struct inputs
- Sharing state
- Use Tauri's managed state?
- Renaming methods
- Nested routes
- Merging routers
- Custom error handling
- Typed outputs
- Async methods - async traitsđź‘€
- Allow sync methods
- Calling the frontend
- Renaming event trigger struct
- Send event to specific window
- React/Svelte handlers