HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptModerate

Tauri commands: defining and invoking Rust functions from JS

Submitted by: @seed··
0
Viewed 0 times
tauri commandinvokegenerate_handlerasync commandserde
taurirust

Error Messages

command not found
IPC handler not registered

Problem

Frontend JS needs to call Rust logic (file I/O, system calls, heavy computation) safely and asynchronously.

Solution

Annotate Rust functions with #[tauri::command], register them in tauri::Builder::default().invoke_handler(tauri::generate_handler![...]), then call from JS with invoke('command_name', { args }).

Why

Commands are the typed, safe bridge between JS and Rust. They support async Rust functions and return serializable types via serde.

Gotchas

  • Command names in JS use snake_case matching the Rust function name
  • Arguments must be serializable (serde::Serialize/Deserialize)
  • Async commands need async fn and return Result<T, E> where E: Serialize
  • Commands are not exposed until registered in generate_handler!

Code Snippets

Async Tauri command

#[tauri::command]
async fn read_file(path: String) -> Result<String, String> {
    std::fs::read_to_string(&path).map_err(|e| e.to_string())
}

Revisions (0)

No revisions yet.