How do I send data from rust (main.rs) to the front-end using vanilla js?
I'm using a rust ping library that can work across different operating systems.
https://github.com/orf/gping/blob/master/pinger/src/lib.rs#L7-L17
I've used create-tauri-app on Windows (npm create tauri-app@latest)
✔ Project name · tauri-app
✔ Choose your package manager · npm
✔ Choose your UI template · vanilla
This is how my main function looks like (for now). I'm able to ping while GUI app is displaying and not hanging:
fn main() {
tauri::Builder::default()
.setup(|_app| {
thread::spawn(move || {
let stream = ping("google.com".to_string(), None).expect("Error pinging");
for message in stream {
match message {
PingResult::Pong(duration, _line) => {
// Update front-end
println!("ping={:#?}", duration);
}
PingResult::Timeout(_) => {
println!("Timeout!");
}
PingResult::Unknown(_) => (),
PingResult::PingExited(e, stderr) => {
println!("There was an error running ping: {e}\nStderr: {stderr}\n");
break;
}
}
}
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}