10 releases (6 breaking)

new 0.8.0 Jan 7, 2025
0.6.0 Mar 27, 2024
0.5.2 Oct 5, 2023
0.5.0 Jul 18, 2023

#361 in WebSocket

Download history 38/week @ 2024-09-22 19/week @ 2024-09-29 25/week @ 2024-10-06 52/week @ 2024-10-13 32/week @ 2024-10-20 20/week @ 2024-10-27 31/week @ 2024-11-03 28/week @ 2024-11-10 64/week @ 2024-11-17 68/week @ 2024-11-24 8/week @ 2024-12-01 41/week @ 2024-12-08 13/week @ 2024-12-15 1/week @ 2024-12-22 114/week @ 2025-01-05

131 downloads per month

MIT license

24KB
337 lines

Leptos Server Signals

Server signals are leptos signals 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.

Feature flags

  • 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.

Example

Cargo.toml

[dependencies]
leptos_server_signal = "*"
serde = { version = "*", features = ["derive"] }

[features]
ssr = [
  "leptos_server_signal/ssr",
  "leptos_server_signal/axum", # or actix
]

Client

use leptos::*;
use leptos_server_signal::create_server_signal;
use serde::{Deserialize, Serialize};

#[derive(Clone, Default, Serialize, Deserialize)]
pub struct Count {
    pub value: i32,
}

#[component]
pub fn App() -> impl IntoView {
    // Provide websocket connection
    leptos_server_signal::provide_websocket("ws://localhost:3000/ws").unwrap();

    // Create server signal
    let count = create_server_signal::<Count>("counter");

    view! {
        <h1>"Count: " {move || count.get().value.to_string()}</h1>
    }
}

Server (Axum)

#[cfg(feature = "ssr")]
pub async fn websocket(ws: WebSocketUpgrade) -> Response {
    ws.on_upgrade(handle_socket)
}

#[cfg(feature = "ssr")]
async fn handle_socket(mut socket: WebSocket) {
    let mut count = ServerSignal::<Count>::new("counter").unwrap();

    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;
        }
    }
}

Connection Retry

With the example above, the connection does not get reestablished after a connection lost. To regularly try to reconnect again, the function provide_websocket_with_retry(...) can be used:

#[component]
pub fn App() -> impl IntoView {
    // Provide websocket connection
    leptos_server_signal::provide_websocket_with_retry(
        "ws://localhost:3000/ws",
        5000, // retry in 5000 milliseconds
    ).unwrap();

    // ... code from above
}

Dependencies

~21–36MB
~581K SLoC