tauri-apps / wry

Cross-platform WebView library in Rust for Tauri.
Apache License 2.0
3.73k stars 284 forks source link

Streaming protocol #1404

Open rgbkrk opened 3 weeks ago

rgbkrk commented 3 weeks ago

Is your feature request related to a problem? Please describe.

I'd love to have a way to push data to the webview in a streaming fashion, whether server sent events or websockets. This got discussed in https://github.com/tauri-apps/wry/issues/420, but is distinctly different as that issue was about asynchronously returning a singular response.

Describe the solution you'd like

Accept Response<Stream<Item = Vec<u8>>> in some manner.

Response (from http) can already accept a Stream

fn stream_response(
    _request: Request<Vec<u8>>,
) -> Result<Response<impl Stream<Item = Vec<u8>> + Send>> {
    let stream = futures::stream::iter(vec![
        "Hello ".as_bytes().to_vec(),
        "World".as_bytes().to_vec(),
    ]);

    Ok(Response::builder()
        .header("Content-Type", "text/plain")
        .status(200)
        .body(stream)
        .unwrap())
}

Where it goes awry is on response via the RequestAsyncResponder, which requires a Response<Into<Cow<'static, [u8]>>>.

image