Menu

Post image 1
Post image 2
1 / 2
0

File Watching in Rust with notify-rs — Hot Folders for a Sync App

DEV Community·hiyoyo·19 days ago
#sW1HnoET
#tauri#rust#webdev#programming#path#watcher
Reading 0:00
15s threshold

All tests run on an 8-year-old MacBook Air. All results from shipping 7 Mac apps as a solo developer. No sponsored opinion. HiyokoAutoSync watches directories for changes and triggers sync automatically. notify-rs is the Rust crate for this. Here's what I learned using it in a shipping Tauri app. Basic setup toml[dependencies] notify = "6" rustuse notify::{RecommendedWatcher, RecursiveMode, Watcher, Config}; use std::sync::mpsc; fn watch_directory(path: &str) -> Result<(), AppError> { let (tx, rx) = mpsc::channel(); let mut watcher = RecommendedWatcher::new(tx, Config::default())?; watcher.watch(path.as_ref(), RecursiveMode::Recursive)?; for res in rx { match res { Ok(event) => handle_event(event), Err(e) => log::error!("Watch error: {:?}", e), } } Ok(()) Enter fullscreen mode Exit fullscreen mode } RecommendedWatcher uses FSEvents on macOS — the native file system event API. Low overhead, fast notification. The double-fire problem File save operations often trigger multiple events.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More