patterntypescriptTip
Tauri event system for real-time communication
Viewed 0 times
tauri eventsemit_alllistenunlistenreal-time pushbackground task
taurirust
Problem
Rust backend needs to push data to the frontend (progress updates, background task results) without the frontend polling via invoke.
Solution
Use app_handle.emit_all('event-name', payload) from Rust to broadcast to all windows. In JS, use listen('event-name', handler) from @tauri-apps/api/event.
Why
Events are push-based and decouple the backend from frontend polling. Useful for long-running tasks, file watchers, and OS notifications.
Gotchas
- emit_all sends to all windows; use emit_to for a specific window label
- JS listeners must be cleaned up: the listen() function returns an unlisten() function
- Payload must implement serde::Serialize
- Events are not guaranteed to arrive in order under heavy load
Code Snippets
Emit event from Rust
app_handle.emit_all("download-progress", serde_json::json!({ "percent": 42 })).unwrap();Listen in frontend
import { listen } from '@tauri-apps/api/event';
const unlisten = await listen<{ percent: number }>('download-progress', (e) => {
console.log(e.payload.percent);
});
// cleanup:
unlisten();Revisions (0)
No revisions yet.