Leptos Server Signals
Server signals are leptos kept in sync with the server through websockets.
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 websocket to the client as [json patches].
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] leptosserversignal = "" serde = { version = "", features = ["derive"] }
[features] ssr = [ "leptosserversignal/ssr", "leptosserversignal/axum", # or actix ] ```
Client
```rust use leptos::*; use leptosserversignal::createserversignal; use serde::{Deserialize, Serialize};
pub struct Count { pub value: i32, }
pub fn App(cx: Scope) -> impl IntoView { // Provide websocket connection leptosserversignal::provide_websocket(cx, "ws://localhost:3000/ws").unwrap();
// Create server signal
let count = create_server_signal::<Count>(cx, "counter");
view! { cx,
<h1>"Count: " {move || count().value.to_string()}</h1>
}
} ```
Server (Axum)
```rust
pub async fn websocket(ws: WebSocketUpgrade) -> Response { ws.onupgrade(handlesocket) }
async fn handle_socket(mut socket: WebSocket) {
let mut count = ServerSignal::
loop {
tokio::time::sleep(Duration::from_millis(10)).await;
let result = count.with(&mut socket, |count| count.value += 1).await;
if result.is_err() {
break;
}
}
} ```