Menu

1 / 2

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

DEV Community·hiyoyo·6 months ago
#sW1HnoET
#tauri#rust#webdev#programming#path
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.