How to hook up my basic app with a tokio task?

I have a simple GUI that was very fun and quick to make with iced (complete GUI noob here). All it has are 5 buttons in a specific layout.

I also have an existing program that runs as a bunch of tokio tasks which consume and produce messages. In my tauri app I am spawning that task and sending/receiving messages to the channel handles stored in the app state.

How can I do this with iced? I see the websocket example but don’t understand it, is there any docs/tutorial doing something similar?

I dont think theres many examples, I had a while figuring it out from the websocket example. The idea is as follows to my understanding:

  • in your update function, use command::perform(future, t), where future is an async function and t is the Message from your message enum. t will automatically take anything returned from your function as an argument, so your Message::t must take the return type of the function.
    I used it in my project, its awful code and just a prototype but here it is if you want another example:
    audiocloud_desktop/src/main.rs at main · AppearedOne/audiocloud_desktop · GitHub
    edit: what version of iced are you using? depending on that you might need to enable the “tokio” feature

I’m on the specific rev recommended by the tutorial.

Thanks for your hints!

multithreading - How to send messages to iced application - Stack Overflow this answer is nice but doesn’t work currently, I’m trying to rewrite it to current iced

Yes, I forgot to mention that, for streams subscriptions should be used, for single requests Command::perform. Haven’t had much experience with streams and Iced though. So if you’re doing a stream then thats definitely the right way, overcomplicated though for single requests.

1 Like