In the doc tokio::sync::mpsc - Rust, it says: "So for sending a message from async to sync, you should use the standard library unbounded channel or crossbeam."
The code above can NOT compile, the compiler throws the following error:
error: future cannot be sent between threads safely
--> src/main.rs:9:5
|
9 | tokio::task::spawn(async move {
| ^^^^^^^^^^^^^^^^^^ future created by async block is not `Send`
|
= help: the trait `Sync` is not implemented for `std::sync::mpsc::Sender<String>`
note: future is not `Send` as this value is used across an await
tx succeeds but tx.clone() fails, why? I really need to use this tx in multiple async functions.
Could you paste it's signature? If subscribe_websocket_dummy, but subscribe_websocket doesn't, then the answer should be in the difference between the two.
I'm guessing this is because you have an immutable reference to the sender somewhere. You should clone it instead.
Alternatively, you can try using the Tokio mpsc channel. It has a blocking_recv method you can use in sync code. (That method was added after I wrote the sentence about blocking channels, which you quoted from the Tokio documentation.)