Skip to main content

live_render

Function live_render 

Source
pub fn live_render(component: impl LiveComponent, ws_url: &str) -> Response
Expand description

Render a LiveComponent as an initial HTML page.

Returns the component HTML wrapped in a minimal shell with the livewire client JS. The client connects via WebSocket and sends events back to the server.

use framework::livewire::{LiveComponent, live_render, live_socket};

#[framework::get("/counter")]
async fn counter_page() -> impl IntoResponse {
    let component = Counter { count: 0 };
    live_render(component, "/ws/counter")
}

// WebSocket handler for the live component
Router::new()
    .route("/ws/counter", get(|ws: WebSocketUpgrade| {
        ws.on_upgrade(|socket| live_socket(socket, Counter { count: 0 }))
    }))