Response Streaming - JavaScript only receiving 1 packet
- Lenguaje dominante
- Rust
- Estrellas
- 24.8k
- Forks
- 1.9k
- Merge medio
- 23 h 10 min
- PR fusionados (30 d)
- 26
Descripción
## 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
Guía de contribución
Evaluación
Este issue todavía no se ha evaluado.