How to send message to iced gui from other thread?

I need to pass message to iced.

I have a synchronous message sender in another module. I need it to be able to convert it into an iced message once it sends it.

I read about subscriptions, but I’m completely unclear how to properly add this ill-fated receiver to the subscription. If I need to convert a send from an external synchronous sender to an iced mpsc, I think I’ve done it.

What I really need is the send_to_iced_gui(iced_message:Msg) function or something similar.

I spent probably 3 days studying subscriptions, their methods and examples - nothing suggests how you can simply send a message to iced.

[code]

fn subscription(&self) → Subscription {
let rx: Receiver = self.rx;

What should be here?

Is it possible to simply get the data, process it and convert it into an event for the iced depending on the data?

}

pub fn iced_run(self) → Result<(), iced::Error> {
let app = iced::application(“…”, MetricaFe::update, MetricaFe::view)
.theme(|gui| gui.theme.clone())
.subscription(|a| a.subscription());

app.run_with(move || (self, iced::Task::none()))

}

[/code]

Why aren’t there examples of how to send a custom message to iced from any module?

There’s an example in the docs:

You might also want to read through the websocket example:

1 link - Subscription in rust: there is no argument provided to “fn some_worker()” in example.

2 link - same problem, no arg to subscription passed

It’s clear that in this situation I can create a global variable, put the receiver there, and simply retrieve it from the global variable, since it’s not clear how it can be passed there normally.

Or start adding the functionality I need to the iced code (“send_to_iced_gui(iced_message:Msg)” method).

Each of these options is bad in its own way: the first is a crutch, the second is most likely reinventing the wheel.

Maybe you’re looking for Subscription::with?

You could also not use a Subscription and use a stream in a Tasks instead.

Subscription::with - there is no examples in docs. It is unclear how it handles the absence of an argument passed to the function “fn some_worker()” in Subscription::run(some_worker).

use a stream in a Tasks instead. - Could you tell me more? It seemed to me that the only way to interact was through subscriptions.

I need an example that will help me understand how I can create a handler thread with a context, read a Receiver<…> from it, and, upon receiving a message from the Receiver<..>, send an message to the iced app.