actix / actix/actix-web

Response Streaming - JavaScript only receiving 1 packet

Offen
#2,910 7 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Rust
Sterne
24.8k
Forks
1.9k
Ø Merge
23 Std. 10 Min.
Gemergte PRs (30 T.)
26

Beschreibung

## 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.