I am building a speech to text app in rust, using tauri 2.0 + react 19 for the frontend.
I would like to know how you think I should structure my project to syncronise the state of the settings in between the different components in the React UI and the rust backend. Once the app is initialized each rust service has to receive the changes in real time.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here’s a pattern that we’ve found works well for a Tauri + React app where both your Rust “services” and your React UI need to stay in sync in real time with a central “settings” object.
By splitting out the core-settings crate, you can share the same Rust types (and serde JSON schemas) across the backend services. You can even generate TypeScript types from it if you like.
1. Define a central Settings type + pub broadcast channel
In /core-settings/src/lib.rs:
rust
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::sync::{broadcast, RwLock};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
pub language: String,
pub sensitivity: f32,
// … add whatever you need
}
// A Tokio broadcast channel so everyone can subscribe
pub type SettingsTx = broadcast::Sender<Settings>;
pub type SettingsRx = broadcast::Receiver<Settings>;
/// A thread‐safe “global” settings store
#[derive(Clone)]
pub struct SettingsStore {
inner: Arc<RwLock<Settings>>,
tx: SettingsTx,
}
impl SettingsStore {
pub fn new(initial: Settings) -> Self {
let (tx, _) = broadcast::channel(16);
let store = SettingsStore {
inner: Arc::new(RwLock::new(initial.clone())),
tx,
};
// seed with the initial value:
let _ = store.tx.send(initial);
store
}
/// read‐only access
pub async fn get(&self) -> Settings {
self.inner.read().await.clone()
}
/// update & notify everybody
pub async fn set(&self, new: Settings) {
*self.inner.write().await = new.clone();
let _ = self.tx.send(new);
}
/// subscribe to updates
pub fn subscribe(&self) -> SettingsRx {
self.tx.subscribe()
}
}
2. Wire it into your Tauri backend
In /backend/src-tauri/main.rs:
rust
use tauri::{Manager, Window};
use core_settings::{Settings, SettingsStore};
use std::sync::OnceLock;
use tokio::runtime::Runtime;
// Make a global runtime + store
static TOKIO_RT: OnceLock<Runtime> = OnceLock::new();
static SETTINGS: OnceLock<SettingsStore> = OnceLock::new();
fn main() {
let rt = Runtime::new().unwrap();
TOKIO_RT.set(rt).unwrap();
// Load persisted settings from disk (or defaults)
let initial = Settings {
language: "en-US".into(),
sensitivity: 0.5,
};
let store = SettingsStore::new(initial);
SETTINGS.set(store).unwrap();
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
get_settings,
update_settings
])
.setup(|app| {
// (optional) if you want to push events out on startup
let app_handle = app.handle();
let store = SETTINGS.get().unwrap().clone();
// spawn a task to forward all settings changes to the front end
app_handle
.state::<SettingsStore>();
TOKIO_RT.get().unwrap().spawn(async move {
let mut rx = store.subscribe();
while let Ok(updated) = rx.recv().await {
app_handle
.emit_all("settings-updated", updated.clone())
.unwrap();
}
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri");
}
#[tauri::command]
async fn get_settings() -> Settings {
SETTINGS.get().unwrap().get().await
}
#[tauri::command]
async fn update_settings(new: Settings) {
SETTINGS.get().unwrap().set(new).await;
// persistence to disk could go here too
}
3. Frontend: keep a React store + subscribe to events
Pick a client‐side store: Context + useReducer, Redux, or lightweight Zustand.
On app boot:
invoke("get_settings") to seed your React store.
window.__TAURI__.event.listen("settings-updated", callback) to merge updates.
Anywhere else in your Rust backend—say you have a speech‐recognition service—you can grab a receiver:
rust
let store = SETTINGS.get().unwrap().clone();
let mut rx = store.subscribe();
tokio::spawn(async move {
while let Ok(settings) = rx.recv().await {
// apply new settings to your recognition pipeline
println!("Speech service got new settings: {:?}", settings);
}
});
Why this works well
Single source of truth in Rust (SettingsStore) that all Rust services subscribe to.
Events (settings-updated) push changes front → back and back → front in real time.
Shared types via a small core-settings crate, so your serde schema stays consistent.
From here you can layer on persistence (JSON file or SQLite) in your Tauri setup or the plugin‐store, but the above gives you a solid, real‐time synchronization topology. 🚀