calling-frontend-from-tauri-rustlisted
Install: claude install-skill Sheshiyer/skill-clusters
# Calling Frontend from Tauri Rust
Tauri provides three mechanisms for Rust to communicate with the frontend: the event system, channels, and JavaScript evaluation.
## Event System Overview
The event system enables bi-directional communication between Rust and frontend. Best for small data transfers and multi-consumer patterns. Not designed for low latency or high throughput.
### Required Imports
```rust
use tauri::{AppHandle, Emitter, Manager, Listener, EventTarget};
use serde::Serialize;
```
```typescript
import { listen, once, emit } from '@tauri-apps/api/event';
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';
```
## Emitting Events from Rust
### Global Events (All Listeners)
Use `AppHandle::emit()` to broadcast to all listeners:
```rust
use tauri::{AppHandle, Emitter};
#[tauri::command]
fn download(app: AppHandle, url: String) {
app.emit("download-started", &url).unwrap();
for progress in [1, 15, 50, 80, 100] {
app.emit("download-progress", progress).unwrap();
}
app.emit("download-finished", &url).unwrap();
}
```
### Webview-Specific Events
Target specific webviews with `emit_to()`:
```rust
use tauri::{AppHandle, Emitter};
#[tauri::command]
fn login(app: AppHandle, user: String, password: String) {
let authenticated = user == "tauri-apps" && password == "tauri";
let result = if authenticated { "loggedIn" } else { "invalidCredentials" };
app.emit_to("login", "login-result", result).unwrap();
}
```