Skip to content

stream body #3005

Answered by piquu
piquu asked this question in Q&A
Oct 23, 2024 · 1 comments · 5 replies
Discussion options

You must be logged in to vote

i got it working. the issue was that i set the channel buffer to usize::MAX. tysm for helping me

pub async fn stream() -> impl IntoResponse {
    let (tx, rx) = tokio::sync::mpsc::channel::<Result<axum::body::Bytes, anyhow::Error>>(100);

    tokio::spawn(async move {
        for i in 1..100 {
            tx.send(Ok(axum::body::Bytes::from(format!("number: {i}")))).await;
            tokio::time::sleep(std::time::Duration::from_millis(500)).await;
        }
    });

    Body::from_stream(tokio_stream::wrappers::ReceiverStream::new(rx))
}

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@mladedav
Comment options

@piquu
Comment options

@mladedav
Comment options

@piquu
Comment options

Answer selected by piquu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants