Actix-web : send HTTP chunked response progressively

With following code, I can send chunked response and client receives it.

fn index_async( _req: HttpRequest) -> impl Future<Item=HttpResponse, Error=Error> {
    
    let (tx, rx_body) = mpsc::unbounded();

    let text = "Something!";
    tx.unbounded_send(Bytes::from(text.as_bytes()));

    ok(HttpResponse::Ok().streaming(rx_body.map_err(|_| error::ErrorBadRequest("bad request"))))
}

If I try to send as below. unbounded_send() returns a success and "Success sent" is printed out. But the client does not receive anything and HTTP connection is halt there.

fn index_async( _req: HttpRequest) -> impl Future<Item=HttpResponse, Error=Error> {
    
    let (tx, rx_body) = mpsc::unbounded();

    let text = "Something!";

    let fu = async move {
        
        loop {
            // we will await here, for now send a hardcode string to test
            let result = tx.unbounded_send(Bytes::from(text.as_bytes()));
            if let Err(e) = result {
                println!("{}", e);
            } else {
                println!("Success sent");
            }
        }
    };
    actix::spawn(fu.unit_error().boxed_local().compat());

 
    ok(HttpResponse::Ok().streaming(rx_body.map_err(|_| error::ErrorBadRequest("bad request"))))
}

What could be the problem? thank you

[dependencies]
actix = "0.8.3"
bytes = "0.4.12"
futures01 = { package = "futures", version = "0.1", optional = false }

[dependencies.actix-web]
version = "1.0.7"
features = ["ssl"]

[dependencies.futures-preview]
version = "0.3.0-alpha.18"
default-features = false
features = ["compat", "async-await", "nightly"]

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.