Server signals are leptos kept in sync with the server through server-sent-events (SSE).
The signals are read-only on the client side, and can be written to by the server. This is useful if you want real-time updates on the UI controlled by the server.
Changes to a signal are sent through a SSE to the client as [json patches].
This project is heavily based on leptosserversignal.
ssr
: ssr is enabled when rendering the app on the server.actix
: integration with the [Actix] web framework.axum
: integration with the [Axum] web framework.Cargo.toml
```toml [dependencies] leptos_sse = "" serde = { version = "", features = ["derive"] }
[features] ssr = [ "leptossse/ssr", "leptossse/axum", # or actix ] ```
Client
```rust use leptos::*; use leptossse::createsse_signal; use serde::{Deserialize, Serialize};
pub struct Count { pub value: i32, }
pub fn App(cx: Scope) -> impl IntoView { // Provide SSE connection leptossse::providesse(cx, "http://localhost:3000/sse").unwrap();
// Create server signal
let count = create_sse_signal::<Count>(cx, "counter");
view! { cx,
<h1>"Count: " {move || count().value.to_string()}</h1>
}
} ```
If on stable, use
count.get().value
instead ofcount().value
.
Server (Axum)
```rust
use { axum::response::sse::{Event, KeepAlive, Sse}, futures::stream::Stream, };
async fn handlesse() -> Sse
let mut value = 0;
let stream = ServerSentEvents::new(
"counter",
stream::repeat_with(move || {
let curr = value;
value += 1;
Ok(Count { value: curr })
})
.throttle(Duration::from_secs(1)),
)
.unwrap();
Sse::new(stream).keep_alive(KeepAlive::default())
} ```