Send data from Rust to Front end via IPC at an extremely high rate #7146
Replies: 1 comment
-
The main solution to this kind of situation is by keeping the bulk of the data in the Rust end and adopting a view of the webview as just a facilitator of input/output. And you never need 400 000 datapoints per second for human readable output. Either that or you can transmit prerendered images instead using the asset protocol or a custom schema handler. Say you render a graph. Humans cant see 400 000 datapoints per second. You can easily just transmit like 100 per second and get flawless performance. Then when the user zooms in you change what data to transmit. Your frontend shouldnt do heavy lifting, and 400 000 datapoints per second I consider to be heavy lifting. |
Beta Was this translation helpful? Give feedback.
-
The reason I am asking this question is because I have a microcontroller which will be sending some real-time data at a really high rate let's suppose, 400,000 datapoints (x,y coordinates in JSON) per second. I know it sounds like a lot. And this data needs to be rendered using a graph. I am pretty new to Tauri eco system and ultimately a complete noob at coding. The main selling point was the fast IPC from Rust to Webview. Is there an alternative?
Just for laughs, I ended up creating some code on the Rust side which randomly generated these datapoints in a separate loop on a different thread and emitted data to the Front end. I tried 2 approaches.
Any advice, thoughts, comments and skull bashing is welcome 😢
`
#[derive(serde::Serialize, serde::Deserialize, Clone)]
struct Coordinates {
x: f64,
y: f64,
}
#[tauri::command]
async fn start_sending_data(window: Window) {
// let mut ct = counter.0.lock().unwrap();
// let mut cods: Vec = vec![];
thread::spawn(move || {
for n in 1..100000 {
let sum: f64 = n as f64 * 10.0;
// cods.push(Coordinates {
// x: n as f64,
// y: f64::sin(sum),
// })
thread::sleep(Duration::new(1, 0));
window
.emit(
"list-updated",
Coordinates {
x: n as f64,
y: f64::sin(sum),
},
)
.unwrap();
}
// window.emit("list-updated", cods).unwrap();
});
}
`
`
#[derive(serde::Serialize, serde::Deserialize, Clone)]
struct Coordinates {
x: f64,
y: f64,
}
#[tauri::command]
fn start_sending_data(window: Window, counter: State) {
let mut ct = counter.0.lock().unwrap();
if *ct == 0 {
*ct += 1;
println!("Spawning a thread to send data");
thread::spawn(move || {
// let mut cods: Vec = vec![];
for n in 1..100000 {
let sum: f64 = n as f64 * 10.0;
// cods.push(Coordinates {
// x: n as f64,
// y: f64::sin(sum),
// })
window
.emit(
"list-updated",
Coordinates {
x: n as f64,
y: f64::sin(sum),
},
)
.unwrap();
}
// window.emit("list-updated", cods).unwrap();
});
} else if *ct > 0 {
println!("Thread has already started");
}
}
`
Beta Was this translation helpful? Give feedback.
All reactions