actix / actix/actix-web

Response Streaming - JavaScript only receiving 1 packet

Aperta
#2,910 7 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Rust
Stelle
24.8k
Fork
1.9k
Merge medio
23h 10m
PR unite (30g)
26

Descrizione

## Rust:
```
use std::convert::Infallible;
use async_stream::stream;
use actix_web::{get, HttpResponse};
use actix_web::web::Bytes;

#[get("/stream")]
pub async fn stream() -> HttpResponse {

HttpResponse::Ok()
.streaming(stream! {
let mut count = 0;
while count < 10 {
yield Ok::<_, Infallible>(Bytes::from(count.to_string()));
thread::sleep(Duration::from_millis(250));
count += 1;
}
})

}
```

## Javascript:
```
async function get_stream() {
const response = await fetch('/stream');
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();

while (true) {
let {value, done} = await reader.read();
if (done) break;
console.log('Received', value);
}

console.log('Response fully received');
}
```

## Expected Behavior:
```
Received 0
Received 1
Received 2
Received 3
Received 4
Received 5
Received 6
Received 7
Received 8
Received 9
stream.js:69 Response fully received
```

## Current Behavior:
```
Received 0123456789
stream.js:69 Response fully received
```

## Context
I'm trying to query a database and send reach row at a time back to the client. I wasn't able to get streaming to properly stream one row at a time. I made this simple example for testing and found that the JavaScript side was receiving all of the data at once.

- I've tested and got the same result in Chrome and Firefox
- Rust Version: rustc 1.64.0 (a55dd71d5 2022-09-19)
- Actix Web Version: 4.2.1
- Server is using rustls for encryption making the web server HTTP2

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.