Share state between gtk4-rs and Rust Axum server

Thank you for awesome open-source work.

I’ve created a websocket server using Rust Axum crate. Now I need an UI, which can show live activity and updates from this server. I’m trying to use gtk4-rs for GUI part. But I’m not really sure how to properly architect this app so that the app state can be shared between and consumed by both the websocket server and GTK. The shared state could be updated by GTK widgets (for storing user inputs) and also by WS server (messages received from connected clients) simultaneously.

I thought of creating a shared struct object which uses Arc<Mutex<T>> but then how does UI get updated (notified for updating itself) when a particular state update is pushed from WS server side. I guess, we can’t store this shared struct in GObject and take advantage of its properties and signals, because it is not thread-safe and thus can’t be shared with WS server.

Another approach is to use channels to communicate updates between GTK and WS server but then we need to manage two copies of state, one for each side. Also, I think we need an mpmc channel here as the communication needs to be bi-directional. This approach seems more practical to me but what about duplicating the state and state getting out of sync. Also, communicating multiple types of updates (messages) with their data won’t be easy.

Please guide me and hopefully provide a simple example code (pseudo code works too). I’m new to GTK and multi-threaded apps in general.

Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.