Bladeren bron

add a streaming example

Evan Almloff 1 jaar geleden
bovenliggende
commit
2952578a2e

+ 20 - 28
Cargo.lock

@@ -773,6 +773,22 @@ dependencies = [
  "tower-service",
 ]
 
+[[package]]
+name = "axum-streaming"
+version = "0.1.0"
+dependencies = [
+ "dioxus",
+ "futures",
+ "futures-util",
+ "once_cell",
+ "serde",
+ "simple_logger",
+ "tokio",
+ "tracing",
+ "tracing-subscriber",
+ "tracing-wasm",
+]
+
 [[package]]
 name = "axum_session"
 version = "0.12.4"
@@ -4562,29 +4578,6 @@ dependencies = [
  "windows-sys 0.52.0",
 ]
 
-[[package]]
-name = "hoot"
-version = "0.1.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "df22a4d90f1b0e65fe3e0d6ee6a4608cc4d81f4b2eb3e670f44bb6bde711e452"
-dependencies = [
- "httparse",
- "log",
-]
-
-[[package]]
-name = "hootbin"
-version = "0.1.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "354e60868e49ea1a39c44b9562ad207c4259dc6eabf9863bf3b0f058c55cfdb2"
-dependencies = [
- "fastrand 2.0.1",
- "hoot",
- "serde",
- "serde_json",
- "thiserror",
-]
-
 [[package]]
 name = "hostname"
 version = "0.3.1"
@@ -7036,9 +7029,9 @@ dependencies = [
 
 [[package]]
 name = "png"
-version = "0.17.11"
+version = "0.17.12"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1f6c3c3e617595665b8ea2ff95a86066be38fb121ff920a9c0eb282abcd1da5a"
+checksum = "78c2378060fb13acff3ba0325b83442c1d2c44fbb76df481160ddc1687cce160"
 dependencies = [
  "bitflags 1.3.2",
  "crc32fast",
@@ -9952,12 +9945,11 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
 
 [[package]]
 name = "ureq"
-version = "2.9.5"
+version = "2.9.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0b52731d03d6bb2fd18289d4028aee361d6c28d44977846793b994b13cdcc64d"
+checksum = "11f214ce18d8b2cbe84ed3aa6486ed3f5b285cf8d8fbdbce9f3f767a724adc35"
 dependencies = [
  "base64",
- "hootbin",
  "log",
  "native-tls",
  "once_cell",

+ 1 - 0
Cargo.toml

@@ -35,6 +35,7 @@ members = [
     "packages/server-macro",
     "packages/fullstack/examples/axum-hello-world",
     "packages/fullstack/examples/axum-router",
+    "packages/fullstack/examples/axum-streaming",
     "packages/fullstack/examples/axum-desktop",
     "packages/fullstack/examples/axum-auth",
     "packages/fullstack/examples/static-hydrated",

+ 2 - 1
packages/fullstack/examples/axum-desktop/src/server.rs

@@ -5,6 +5,7 @@
 
 use axum_desktop::*;
 use dioxus::prelude::*;
+use dioxus_fullstack::server_fn::axum::register_explicit;
 
 #[tokio::main]
 async fn main() {
@@ -18,7 +19,7 @@ async fn main() {
     axum::serve(
         listener,
         axum::Router::new()
-            .register_server_fns("")
+            .register_server_fns()
             .into_make_service(),
     )
     .await

+ 4 - 0
packages/fullstack/examples/axum-streaming/.gitignore

@@ -0,0 +1,4 @@
+dist
+target
+static
+.dioxus

+ 24 - 0
packages/fullstack/examples/axum-streaming/Cargo.toml

@@ -0,0 +1,24 @@
+[package]
+name = "axum-streaming"
+version = "0.1.0"
+edition = "2021"
+publish = false
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+dioxus = { workspace = true, features = ["fullstack"] }
+serde = "1.0.159"
+simple_logger = "4.2.0"
+tracing-wasm = "0.2.1"
+tracing.workspace = true
+tracing-subscriber = "0.3.17"
+futures = "0.3.30"
+tokio = { workspace = true, optional = true }
+futures-util.workspace = true
+once_cell = "1.19.0"
+
+[features]
+default = []
+server = ["dioxus/axum", "tokio"]
+web = ["dioxus/web"]

+ 41 - 0
packages/fullstack/examples/axum-streaming/src/main.rs

@@ -0,0 +1,41 @@
+use dioxus::prelude::*;
+use futures::StreamExt;
+use server_fn::codec::{StreamingText, TextStream};
+
+fn app() -> Element {
+    let mut response = use_signal(String::new);
+
+    rsx! {
+        button {
+            onclick: move |_| async move {
+                response.write().clear();
+                if let Ok(stream) = test_stream().await {
+                    response.write().push_str("Stream started\n");
+                    let mut stream = stream.into_inner();
+                    while let Some(Ok(text)) = stream.next().await {
+                        response.write().push_str(&text);
+                    }
+                }
+            },
+            "Start stream"
+        }
+        "{response}"
+    }
+}
+
+#[server(output = StreamingText)]
+pub async fn test_stream() -> Result<TextStream, ServerFnError> {
+    let (tx, rx) = futures::channel::mpsc::unbounded();
+    tokio::spawn(async move {
+        loop {
+            tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
+            let _ = tx.unbounded_send(Ok("Hello, world!".to_string()));
+        }
+    });
+
+    Ok(TextStream::new(rx))
+}
+
+fn main() {
+    launch(app)
+}