I have an iced application that I want to control through a web UI. For this I use axum and spawn it in a different tokio task. I would like to send a message from the axum endpoint to the iced application, but I can neither invoke the update method of the iced application nor can I listen to the crossbeam channel inside the iced application.
My code looks like this right now:
mod messages;
mod ui;
mod web;
use crossbeam_channel::unbounded;
use iced::Application;
use iced::Settings;
use tokio;
#[tokio::main]
async fn main() -> iced::Result {
let (s, r) = unbounded::<messages::WebToUIMessage>();
let server_handle = tokio::task::spawn(async { web::run_server(s).await });
let result = ui::MediaHub::run(Settings::default());
// We abort the server when the UI finishes
server_handle.abort();
result
}
You can pass a channel receiver through a Flags struct when starting an iced app. Then in the subscription function use iced::subscription::unfold to convert the received channel messages into iced messages that the UI will respond to. Runnable code is as follows: