🌐
Elf Sternberg
elfsternberg.com › blog › axum-sse-remote-heartbeat
Elf Sternberg: Async Rust: Server-Side Events with a Remote Heartbeat
What I've been trying to get to is a simple, Async Rust codebase that would allow me to plug in web-based server-side events or web socket events and then send them over to one or more clients as needed. A lot of this code comes from a variety of examples that I stitched together, and getting them to work well was a headache, so now you get to share in it. Since this is an async project, I'm going to use Tokio as my runtime and Axum ...
🌐
docs.rs
docs.rs › tokio › latest › tokio › sync › mpsc › index.html
tokio::sync::mpsc - Rust
A multi-producer, single-consumer queue for sending values between asynchronous tasks.
🌐
Stack Overflow
stackoverflow.com › questions › 65452692 › rust-lazy-static-and-tokiosyncmpscchannel-in-tokioselect
asynchronous - Rust lazy_static and tokio::sync::mpsc::channel ...

If you make the following changes to your first example, it should work:

  1. Replace tokio::sync::Mutex with std::sync::Mutex so you don't have to use try_lock in the callback.
  2. Do not store the receiver in the mutex, only the sender.
  3. In the callback, either use an unbounded channel, or make sure to release the lock before sending.
  4. Run the blocking C code on a dedicated thread with std::thread::spawn rather than in tokio::spawn. (why?)

To not store the receiver in the mutex, you can do this:

static ref BROADCAST_CONNECT: Mutex<Option<Sender<bool>>> = Mutex::new(None);

// in main
let (send, recv) = channel(128);
*BROADCAST_CONNECT.lock().unwrap() = Some(send);

If you want a bounded channel, you can release the lock by first cloning the channel, then calling drop on the lock, and then using blocking_send to send. With an unbounded channel, this doesn't matter as sending is instant.

// in C callback
let lock = BROADCAST_CONNECT.lock().unwrap();
let send = lock.as_ref().clone();
drop(lock);
send.blocking_send(...);
Answer from Alice Ryhl on stackoverflow.com
🌐
Reddit
reddit.com › r/rust › need an async mpsc channel that acts as a lossy ring buffer
r/rust on Reddit: Need an async MPSC channel that acts as a lossy ...

What you are asking for doesn’t make any sense. To keep the ordering reasonable, you would need to consume values when writing which violates the sc part of the queue. Would need an mpmc queue to do what you want and consume values when it’s full so you can add the new value.

What type of data are you trying to send? To be as fast as possible, a ring buffer of bytes to store the messages and then sending it them in bulk would be the fastest. Not sure how to make this lossy besides the obvious way of have multiple queues.

🌐
GitHub
github.com › tokio-rs › axum › discussions › 2773
Does axum provide built-in client side library? · tokio-rs/axum ...

axum is for building HTTP servers, it does not provide a client.

I think a closer equivalent to aiohttp would be hyper on which both axum and reqwest is built.

It's safe to use reqwest inside axum handlers, just note that some types may have the same name but actually be different (e.g. if you wanted to take Response from reqwest and return that from an axum handler).

Author: tokio-rs
🌐
Tokio
tokio.rs › blog › 2022-02-announcing-tokio-metrics
Announcing Tokio Metrics 0.1 | Tokio - An asynchronous Rust runtime
Tokio is a runtime for writing reliable asynchronous applications with Rust. It provides async I/O, networking, scheduling, timers, and more.
🌐
Crates
crates.io › crates › thingbuf
crates.io: Rust Package Registry
For full functionality of this site it is necessary to enable JavaScript
🌐
GitHub
github.com › tokio-rs › axum › blob › main › examples › testing-websockets › src › main.rs
axum/examples/testing-websockets/src/main.rs at main · tokio-rs/axum
Ergonomic and modular web framework built with Tokio, Tower, and Hyper - axum/examples/testing-websockets/src/main.rs at main · tokio-rs/axum
Author: tokio-rs
🌐
Reddit
reddit.com › r/rust › tokio axum with david pedersen
r/rust on Reddit: Tokio Axum with David Pedersen

I’m here if anyone has follow up questions or just general questions about axum.

🌐
reddit
reddit.com › r › rust › comments › 5xaxhb › future_based_mpsc_queue_example_with_tokio
r/rust - Future Based mpsc Queue Example with Tokio
192k members in the rust community. A place for all things related to the Rust programming language—an open-source systems language that emphasizes …
🌐
Rust-lang
users.rust-lang.org › help
Trouble using tokio MPSC channel - help - The Rust Programming ...
Your channel buffer is rather large, and the other two task aren't doing much "real" work. As a result, they will complete their work really fast, throwing all results into the channel, and leave you waiting on the main task. So you should either: Shrink your channel buffer, which makes your ot… Answer from zirconium-n on users.rust-lang.org
🌐
Rust
users.rust-lang.org › help
How to use tokio::mpsc::channel in async functions? - help - Rust
January 10, 2020 - How to use tokio::mpsc::channel in async functions · Some errors have detailed explanations: E0277, E0308. For more information about an error, try rustc --explain E0277. error: could not compile hypertest · I finally have a working example, It might be useful to anyone reading this thread.
🌐
Reddit
reddit.com › r/rust › tokio graceful shutdown
r/rust on Reddit: Tokio graceful shutdown

Your post title made me think you were inquiring about tokio-graceful-shutdown, which implements a tree-like structure for cancellation, with timeout and error propagation.

Edit: I'm not sure it will achieve "bottom up" cancellation naturally, but it might be amenable? Its API is more oriented to shutting down a sub-tree.

🌐
Stack Overflow
stackoverflow.com › questions › 74784764 › unexpected-close-of-tokio-mpsc-channel
asynchronous - Unexpected close of tokio mpsc channel - Stack Overflow
I am trying to use tokio::mpsc::channel to send data from a synchronous function to tokio thread to handle it asynchronously. Since tokio::mpsc::channel is an async function, I spawn a runtime from...
🌐
Reddit
reddit.com › r/rust › tokio-axum or actix-web ?
r/rust on Reddit: tokio-axum or actix-web ?

Either will work for your use case. Both are good web frameworks in Rust. Actix happens to be older than Axum, but both should provide you the building blocks you need to do what you want. I've used both. Take a look at the documentation for each. Whichever one looks better to your eyes is probably sufficient to jump up and get started.

🌐
Reddit
reddit.com › r/rust › tokio-axum ou actix-web ?
r/rust on Reddit: tokio-axum ou actix-web ?

L’un ou l’autre fonctionnera pour votre cas d’utilisation. Les deux sont de bons frameworks Web dans Rust. Actix se trouve être plus ancien que Axum, mais les deux devraient vous fournir les éléments de base dont vous avez besoin pour faire ce que vous voulez. J'ai utilisé les deux. Jetez un œil à la documentation de chacun. Celui qui vous semble le mieux est probablement suffisant pour vous lancer et commencer.

🌐
Rust-lang
doc.rust-lang.org › book › ch20-03-graceful-shutdown-and-cleanup.html
Graceful Shutdown and Cleanup - The Rust Programming Language
The code in Listing 20-20 is responding to requests asynchronously through the use of a thread pool, as we intended. We get some warnings about the workers, id, and thread fields that we’re not using in a direct way that reminds us we’re not cleaning up anything.
🌐
Reddit
reddit.com › r/rust › mapping tokio receiver to receiver>
Mapping Tokio Receiver to Receiver : r/rust

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6e748c0be222dbef09139d66997d02ae

🌐
GitHub
github.com › tokio-rs › axum › discussions › 3005
stream body · tokio-rs/axum · Discussion #3005
I believe part of the message should ... with `RUST_BACKTRACE=1` environment variable to display a backtrace. You need to use that to figure out which function you call is panicking and then read the documentation for that function. I'm sorry, but you have to do these steps yourself. Beta Was this translation helpful? Give feedback. ... Something went wrong. ... pub async fn stream() -> impl IntoResponse { let (tx, rx) = tokio::sync::mpsc::channel::<Result<axum::body::Bytes, ...
Author: tokio-rs