Skip to content

The tauri framework library for pure rust frontend

License

Notifications You must be signed in to change notification settings

nanoqsh/tauri-wasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tauri-wasm

The tauri framework library for pure rust frontend with wasm-bindgen

About

Interact with a Tauri backend using the pure Rust library. You don't need NPM or any other JavaScript tools to build a frontend, use Cargo instead.

This crate is designed for Tauri version 2.0 and above.

Getting Started

Let's write the frontend part:

use {
    gloo::console,
    wasm_bindgen::prelude::*,
};

#[wasm_bindgen]
pub async fn start() -> Result<(), JsError> {
    // first, check if we are running in a Tauri environment
    if !tauri_wasm::is_tauri() {
        console::error!("tauri was not detected!");
        return Ok(());
    }

    // invoke the "hello" command on the backend
    let message = tauri_wasm::invoke("hello").await?;

    // log the response from the backend
    console::log!("message: ", message);

    Ok(())
}

When wasm_bindgen attribute exports our start function to the JS runtime, we can call it from frontend.

On the backend part let's write this:

// define the "hello" command
#[tauri::command]
fn hello() -> String {
    println!("frontend invoked hello");
    "message from backend".to_owned()
}

// configure the backend
pub fn run() {
    tauri::Builder::default()
        // register the "hello" command
        .invoke_handler(tauri::generate_handler![hello])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

For more details, see the example in the repository.

About

The tauri framework library for pure rust frontend

Topics

Resources

License

Stars

Watchers

Forks