Met à jour les listes en direct via WebSocket

Le serveur prévient, sur /ws, le propriétaire et les membres d'une liste
dès qu'elle change ; leurs pages rechargent alors leurs données.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Romulus21andClaude Opus 5.5 committed 2026-10-11 13:32:37 +02:00
1 parent 9f3fd5701e
commit 2ade50465e
10 files changed
+359 -38

No files matched your search

Generated
+2
View File
@@ -1402,11 +1402,13 @@ dependencies = [
"leptos_meta",
"leptos_router",
"rand_core 0.6.4",
"send_wrapper",
"serde",
"sha2",
"sqlx",
"tokio",
"wasm-bindgen",
"web-sys",
]
[[package]]
+11 -3
View File
@@ -11,8 +11,8 @@ leptos = { version = "0.8" }
leptos_router = { version = "0.8" }
leptos_meta = { version = "0.8" }
leptos_axum = { version = "0.8", optional = true }
axum = { version = "0.8", optional = true }
tokio = { version = "1", features = ["rt-multi-thread", "macros"], optional = true }
axum = { version = "0.8", features = ["ws"], optional = true }
tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "time"], optional = true }
sqlx = { version = "0.8", default-features = false, features = ["sqlite", "runtime-tokio", "macros", "migrate"], optional = true }
argon2 = { version = "0.5", optional = true }
rand_core = { version = "0.6", features = ["getrandom"], optional = true }
@@ -21,10 +21,18 @@ hex = { version = "0.4", optional = true }
dotenvy = { version = "0.15", optional = true }
serde = { version = "1", features = ["derive"] }
wasm-bindgen = { version = "=0.2.128", optional = true }
web-sys = { version = "0.3", features = ["CloseEvent", "Document", "Location", "MessageEvent", "VisibilityState", "WebSocket", "Window"], optional = true }
send_wrapper = { version = "0.6", optional = true }
console_error_panic_hook = { version = "0.1", optional = true }
[features]
hydrate = ["leptos/hydrate", "dep:console_error_panic_hook", "dep:wasm-bindgen"]
hydrate = [
"leptos/hydrate",
"dep:console_error_panic_hook",
"dep:wasm-bindgen",
"dep:web-sys",
"dep:send_wrapper",
]
ssr = [
"dep:axum",
"dep:tokio",
+50 -15
View File
@@ -48,13 +48,32 @@ mod server {
}
}
/// Fails unless the item belongs to a list the user can access.
pub async fn require_item(pool: &SqlitePool, user: &User, item_id: i64) -> Result<(), ServerFnError> {
/// Fails unless the item belongs to a list the user can access; returns that list.
pub async fn require_item(pool: &SqlitePool, user: &User, item_id: i64) -> Result<i64, ServerFnError> {
let list_id = crate::db::item_list_id(pool, item_id)
.await
.map_err(db_err)?
.ok_or_else(|| ServerFnError::new("Article introuvable."))?;
require_list(pool, user, list_id).await.map(|_| ())
require_list(pool, user, list_id).await?;
Ok(list_id)
}
/// Everyone who can see the list, to pass to [`notify`] once it changed.
pub async fn audience(pool: &SqlitePool, list_id: i64) -> Result<Vec<i64>, ServerFnError> {
crate::db::list_audience(pool, list_id).await.map_err(db_err)
}
/// Tells these users' open pages to refetch.
pub fn notify(user_ids: Vec<i64>) {
if let Some(hub) = use_context::<crate::live::Hub>() {
hub.notify(user_ids);
}
}
/// Notifies everyone who can see the list.
pub async fn notify_list(pool: &SqlitePool, list_id: i64) -> Result<(), ServerFnError> {
notify(audience(pool, list_id).await?);
Ok(())
}
pub fn clean_name(name: &str, max: usize) -> Result<String, ServerFnError> {
@@ -167,7 +186,9 @@ pub async fn create_list(name: String, color: ListColor) -> Result<i64, ServerFn
let pool = pool()?;
let user = require_user(&pool).await?;
let name = clean_name(&name, 60)?;
crate::db::create_list(&pool, user.id, &name, color).await.map_err(db_err)
let id = crate::db::create_list(&pool, user.id, &name, color).await.map_err(db_err)?;
notify(vec![user.id]);
Ok(id)
}
#[server]
@@ -177,7 +198,8 @@ pub async fn rename_list(id: i64, name: String) -> Result<(), ServerFnError> {
let user = require_user(&pool).await?;
require_owner(&pool, &user, id).await?;
let name = clean_name(&name, 60)?;
crate::db::rename_list(&pool, id, &name).await.map_err(db_err)
crate::db::rename_list(&pool, id, &name).await.map_err(db_err)?;
notify_list(&pool, id).await
}
#[server]
@@ -186,7 +208,10 @@ pub async fn delete_list(id: i64) -> Result<(), ServerFnError> {
let pool = pool()?;
let user = require_user(&pool).await?;
require_owner(&pool, &user, id).await?;
crate::db::delete_list(&pool, id).await.map_err(db_err)
let audience = audience(&pool, id).await?;
crate::db::delete_list(&pool, id).await.map_err(db_err)?;
notify(audience);
Ok(())
}
#[server]
@@ -212,7 +237,8 @@ pub async fn share_list(list_id: i64, email: String) -> Result<(), ServerFnError
if friend.id == user.id {
return Err(ServerFnError::new("Tu es déjà propriétaire de cette liste."));
}
crate::db::add_member(&pool, list_id, friend.id).await.map_err(db_err)
crate::db::add_member(&pool, list_id, friend.id).await.map_err(db_err)?;
notify_list(&pool, list_id).await
}
/// The owner removes someone, or a member leaves (`user_id` = themself).
@@ -224,7 +250,11 @@ pub async fn remove_member(list_id: i64, user_id: i64) -> Result<(), ServerFnErr
if user_id != user.id {
require_owner(&pool, &user, list_id).await?;
}
crate::db::remove_member(&pool, list_id, user_id).await.map_err(db_err)
// Computed before, so the removed member hears about it too.
let audience = audience(&pool, list_id).await?;
crate::db::remove_member(&pool, list_id, user_id).await.map_err(db_err)?;
notify(audience);
Ok(())
}
// --- Items ------------------------------------------------------------------
@@ -245,7 +275,9 @@ pub async fn add_item(list_id: i64, input: ItemInput) -> Result<i64, ServerFnErr
let user = require_user(&pool).await?;
require_list(&pool, &user, list_id).await?;
let input = ItemInput { name: clean_name(&input.name, 60)?, ..input };
crate::db::add_item(&pool, user.id, list_id, &input).await.map_err(db_err)
let id = crate::db::add_item(&pool, user.id, list_id, &input).await.map_err(db_err)?;
notify_list(&pool, list_id).await?;
Ok(id)
}
#[server(input = Json)]
@@ -253,9 +285,10 @@ pub async fn update_item(id: i64, input: ItemInput) -> Result<(), ServerFnError>
use server::*;
let pool = pool()?;
let user = require_user(&pool).await?;
require_item(&pool, &user, id).await?;
let list_id = require_item(&pool, &user, id).await?;
let input = ItemInput { name: clean_name(&input.name, 60)?, ..input };
crate::db::update_item(&pool, id, &input).await.map_err(db_err)
crate::db::update_item(&pool, id, &input).await.map_err(db_err)?;
notify_list(&pool, list_id).await
}
#[server]
@@ -263,8 +296,9 @@ pub async fn delete_item(id: i64) -> Result<(), ServerFnError> {
use server::*;
let pool = pool()?;
let user = require_user(&pool).await?;
require_item(&pool, &user, id).await?;
crate::db::delete_item(&pool, id).await.map_err(db_err)
let list_id = require_item(&pool, &user, id).await?;
crate::db::delete_item(&pool, id).await.map_err(db_err)?;
notify_list(&pool, list_id).await
}
#[server]
@@ -272,8 +306,9 @@ pub async fn set_checked(id: i64, checked: bool) -> Result<(), ServerFnError> {
use server::*;
let pool = pool()?;
let user = require_user(&pool).await?;
require_item(&pool, &user, id).await?;
crate::db::set_checked(&pool, id, checked).await.map_err(db_err)
let list_id = require_item(&pool, &user, id).await?;
crate::db::set_checked(&pool, id, checked).await.map_err(db_err)?;
notify_list(&pool, list_id).await
}
#[server]
+140
View File
@@ -0,0 +1,140 @@
//! Live updates (browser side): a WebSocket on `/ws` bumps `refresh` whenever
//! someone changes a list this user can see.
use leptos::prelude::*;
/// Listens for changes until the calling component is unmounted.
#[cfg_attr(not(feature = "hydrate"), allow(unused_variables))]
pub fn subscribe(refresh: RwSignal<u32>) {
#[cfg(feature = "hydrate")]
browser::subscribe(refresh);
}
#[cfg(feature = "hydrate")]
mod browser {
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;
use leptos::prelude::*;
use send_wrapper::SendWrapper;
use wasm_bindgen::JsCast;
use wasm_bindgen::closure::Closure;
use web_sys::{VisibilityState, WebSocket};
const MAX_DELAY: Duration = Duration::from_secs(30);
type Handler = Closure<dyn FnMut(web_sys::Event)>;
#[derive(Default)]
struct Live {
socket: Option<WebSocket>,
/// Kept alive as long as the socket that calls them.
handlers: Vec<Handler>,
/// Failed attempts since the last successful connection.
retries: u32,
stopped: bool,
}
pub fn subscribe(refresh: RwSignal<u32>) {
let live = Rc::new(RefCell::new(Live::default()));
connect(&live, refresh);
// Phones freeze background tabs and may silently drop the socket:
// catch up whenever the page comes back.
let on_visible: Handler = Closure::new(move |_| {
if document().visibility_state() == VisibilityState::Visible {
bump(refresh);
}
});
_ = document()
.add_event_listener_with_callback("visibilitychange", on_visible.as_ref().unchecked_ref());
let cleanup = SendWrapper::new((live, on_visible));
on_cleanup(move || {
let (live, on_visible) = cleanup.take();
_ = document().remove_event_listener_with_callback(
"visibilitychange",
on_visible.as_ref().unchecked_ref(),
);
let mut live = live.borrow_mut();
live.stopped = true;
if let Some(socket) = live.socket.take() {
detach(&socket);
_ = socket.close();
}
live.handlers.clear();
});
}
fn connect(live: &Rc<RefCell<Live>>, refresh: RwSignal<u32>) {
let location = window().location();
let scheme = match location.protocol().as_deref() {
Ok("https:") => "wss",
_ => "ws",
};
let host = location.host().unwrap_or_default();
let Ok(socket) = WebSocket::new(&format!("{scheme}://{host}/ws")) else {
retry_later(live, refresh);
return;
};
let on_open: Handler = {
let live = Rc::clone(live);
Closure::new(move |_| {
// After a reconnection, changes made meanwhile were missed.
if std::mem::take(&mut live.borrow_mut().retries) > 0 {
bump(refresh);
}
})
};
let on_message: Handler = Closure::new(move |_| bump(refresh));
let on_close: Handler = {
let live = Rc::clone(live);
Closure::new(move |_| retry_later(&live, refresh))
};
socket.set_onopen(Some(on_open.as_ref().unchecked_ref()));
socket.set_onmessage(Some(on_message.as_ref().unchecked_ref()));
socket.set_onclose(Some(on_close.as_ref().unchecked_ref()));
let mut state = live.borrow_mut();
state.socket = Some(socket);
// The previous socket's handlers are no longer called by anyone.
state.handlers = vec![on_open, on_message, on_close];
}
/// Reconnects with exponential backoff: 1 s, 2 s, 4 s… up to 30 s.
fn retry_later(live: &Rc<RefCell<Live>>, refresh: RwSignal<u32>) {
let delay = {
let mut state = live.borrow_mut();
if state.stopped {
return;
}
if let Some(socket) = state.socket.take() {
detach(&socket);
}
let delay = Duration::from_secs(1 << state.retries.min(5)).min(MAX_DELAY);
state.retries += 1;
delay
};
let live = Rc::clone(live);
set_timeout(
move || {
if !live.borrow().stopped {
connect(&live, refresh);
}
},
delay,
);
}
fn detach(socket: &WebSocket) {
socket.set_onopen(None);
socket.set_onmessage(None);
socket.set_onclose(None);
}
fn bump(refresh: RwSignal<u32>) {
refresh.try_update(|n| *n += 1);
}
}
+25 -9
View File
@@ -1,5 +1,6 @@
mod account;
mod icons;
mod live;
mod sheets;
mod tiles;
@@ -82,8 +83,9 @@ pub struct Ctx {
/// Bumped after every mutation so resources refetch.
refresh: RwSignal<u32>,
error: RwSignal<Option<String>>,
/// Checked state applied before the server confirms it.
optimistic: RwSignal<HashMap<i64, bool>>,
/// Checked state applied before fresh data shows it, with the number of
/// calls still in flight for that item.
optimistic: RwSignal<HashMap<i64, (bool, u32)>>,
item_editor: RwSignal<Option<ItemEditor>>,
list_editor: RwSignal<Option<ListEditor>>,
account_open: RwSignal<bool>,
@@ -121,14 +123,22 @@ impl Ctx {
fn toggle(self, item: &Item) {
let (id, checked) = (item.id, !item.checked);
self.optimistic.update(|map| {
map.insert(id, checked);
let in_flight = map.get(&id).map_or(0, |&(_, n)| n);
map.insert(id, (checked, in_flight + 1));
});
spawn_local(async move {
if let Err(err) = crate::api::set_checked(id, checked).await {
self.optimistic.update(|map| {
map.remove(&id);
});
self.report(err);
match crate::api::set_checked(id, checked).await {
Ok(()) => self.optimistic.update_untracked(|map| {
if let Some((_, in_flight)) = map.get_mut(&id) {
*in_flight -= 1;
}
}),
Err(err) => {
self.optimistic.update(|map| {
map.remove(&id);
});
self.report(err);
}
}
});
}
@@ -159,6 +169,7 @@ fn ListsView(user: User) -> impl IntoView {
auth: expect_context::<Auth>(),
};
provide_context(ctx);
live::subscribe(ctx.refresh);
let params = use_params_map();
let active_id = Memo::new(move |_| {
@@ -261,6 +272,11 @@ fn ListsContent(
.into_any();
};
// Fresh data includes every confirmed toggle: only keep pending ones, so
// changes made by others since are not masked.
ctx.optimistic
.update_untracked(|map| map.retain(|_, &mut (_, in_flight)| in_flight > 0));
// Items with pending checkbox changes applied, by category then name.
let mut items = items;
items.sort_by_cached_key(Item::sort_key);
@@ -272,7 +288,7 @@ fn ListsContent(
.iter()
.cloned()
.map(|mut item| {
if let Some(&checked) = optimistic.get(&item.id) {
if let Some(&(checked, _)) = optimistic.get(&item.id) {
item.checked = checked;
}
item
+16 -5
View File
@@ -4,7 +4,7 @@ use std::sync::OnceLock;
use argon2::password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString};
use argon2::Argon2;
use axum::http::{HeaderValue, header, request::Parts};
use axum::http::{HeaderMap, HeaderValue, header, request::Parts};
use leptos::prelude::*;
use leptos_axum::ResponseOptions;
use rand_core::{OsRng, RngCore};
@@ -44,9 +44,11 @@ fn hash_token(token: &str) -> String {
}
fn session_token() -> Option<String> {
let parts = use_context::<Parts>()?;
parts
.headers
token_from_headers(&use_context::<Parts>()?.headers)
}
fn token_from_headers(headers: &HeaderMap) -> Option<String> {
headers
.get_all(header::COOKIE)
.iter()
.filter_map(|value| value.to_str().ok())
@@ -85,7 +87,16 @@ pub async fn end_session(pool: &SqlitePool) -> Result<(), sqlx::Error> {
}
pub async fn current_user(pool: &SqlitePool) -> Result<Option<User>, sqlx::Error> {
match session_token() {
user_for_token(pool, session_token()).await
}
/// Same as [`current_user`], outside of a Leptos request (plain axum handlers).
pub async fn user_from_headers(pool: &SqlitePool, headers: &HeaderMap) -> Result<Option<User>, sqlx::Error> {
user_for_token(pool, token_from_headers(headers)).await
}
async fn user_for_token(pool: &SqlitePool, token: Option<String>) -> Result<Option<User>, sqlx::Error> {
match token {
Some(token) => crate::db::user_by_session(pool, &hash_token(&token)).await,
None => Ok(None),
}
+14
View File
@@ -258,6 +258,20 @@ pub async fn remove_member(pool: &SqlitePool, list_id: i64, user_id: i64) -> Res
Ok(())
}
/// Everyone who can see the list: its owner and members.
pub async fn list_audience(pool: &SqlitePool, list_id: i64) -> Result<Vec<i64>, sqlx::Error> {
sqlx::query(
"SELECT owner_id AS user_id FROM lists WHERE id = ?1 AND owner_id IS NOT NULL
UNION SELECT user_id FROM list_members WHERE list_id = ?1",
)
.bind(list_id)
.fetch_all(pool)
.await?
.iter()
.map(|row| row.try_get("user_id"))
.collect()
}
// --- Items ------------------------------------------------------------------
pub async fn items(pool: &SqlitePool, list_id: i64) -> Result<Vec<Item>, sqlx::Error> {
+2
View File
@@ -6,6 +6,8 @@ pub mod model;
pub mod auth;
#[cfg(feature = "ssr")]
pub mod db;
#[cfg(feature = "ssr")]
pub mod live;
#[cfg(feature = "hydrate")]
#[wasm_bindgen::prelude::wasm_bindgen]
+81
View File
@@ -0,0 +1,81 @@
//! Live updates (server only): open pages keep a WebSocket on `/ws` and are
//! told to refetch whenever a list their user can see changes.
use std::sync::Arc;
use std::time::Duration;
use axum::Extension;
use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade};
use axum::http::{HeaderMap, StatusCode};
use axum::response::{IntoResponse, Response};
use sqlx::SqlitePool;
use tokio::sync::broadcast::{self, error::RecvError};
/// Sent to the browser; the payload carries no data, pages refetch through
/// the usual server functions.
const REFRESH: &str = "refresh";
/// Keeps idle connections alive through the reverse proxy.
const PING_EVERY: Duration = Duration::from_secs(30);
/// Fans changes out to every open WebSocket.
#[derive(Clone)]
pub struct Hub(broadcast::Sender<Arc<[i64]>>);
impl Hub {
pub fn new() -> Self {
Self(broadcast::channel(64).0)
}
/// Tells these users' open pages to refetch.
pub fn notify(&self, user_ids: Vec<i64>) {
// Fails only when nobody is connected.
let _ = self.0.send(user_ids.into());
}
}
impl Default for Hub {
fn default() -> Self {
Self::new()
}
}
pub async fn handler(
ws: WebSocketUpgrade,
headers: HeaderMap,
Extension(pool): Extension<SqlitePool>,
Extension(hub): Extension<Hub>,
) -> Response {
match crate::auth::user_from_headers(&pool, &headers).await {
Ok(Some(user)) => {
let events = hub.0.subscribe();
ws.on_upgrade(move |socket| serve(socket, events, user.id))
}
Ok(None) => StatusCode::UNAUTHORIZED.into_response(),
Err(err) => {
leptos::logging::error!("database error: {err}");
StatusCode::INTERNAL_SERVER_ERROR.into_response()
}
}
}
async fn serve(mut socket: WebSocket, mut events: broadcast::Receiver<Arc<[i64]>>, user_id: i64) {
let mut ping = tokio::time::interval(PING_EVERY);
loop {
let outgoing = tokio::select! {
event = events.recv() => match event {
Ok(users) if !users.contains(&user_id) => continue,
// Missed events: refetching covers them too.
Ok(_) | Err(RecvError::Lagged(_)) => Message::Text(REFRESH.into()),
Err(RecvError::Closed) => break,
},
_ = ping.tick() => Message::Ping(Default::default()),
incoming = socket.recv() => match incoming {
Some(Ok(_)) => continue,
_ => break,
},
};
if socket.send(outgoing).await.is_err() {
break;
}
}
}
+18 -6
View File
@@ -1,10 +1,12 @@
#[cfg(feature = "ssr")]
#[tokio::main]
async fn main() {
use axum::Router;
use axum::routing::get;
use axum::{Extension, Router};
use leptos::prelude::*;
use leptos_axum::{LeptosRoutes, generate_route_list};
use lisette::app::{App, shell};
use lisette::live::{self, Hub};
dotenvy::dotenv().ok();
@@ -17,15 +19,20 @@ async fn main() {
let pool = lisette::db::connect(&database_url)
.await
.expect("cannot open database");
let hub = Hub::new();
let routes = generate_route_list(App);
let app = Router::new()
.route("/ws", get(live::handler))
.leptos_routes_with_context(
&leptos_options,
routes,
{
let pool = pool.clone();
move || provide_context(pool.clone())
let (pool, hub) = (pool.clone(), hub.clone());
move || {
provide_context(pool.clone());
provide_context(hub.clone());
}
},
{
let leptos_options = leptos_options.clone();
@@ -34,12 +41,17 @@ async fn main() {
)
.fallback(leptos_axum::file_and_error_handler_with_context(
{
let pool = pool.clone();
move || provide_context(pool.clone())
let (pool, hub) = (pool.clone(), hub.clone());
move || {
provide_context(pool.clone());
provide_context(hub.clone());
}
},
shell,
))
.with_state(leptos_options);
.with_state(leptos_options)
.layer(Extension(pool))
.layer(Extension(hub));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
leptos::logging::log!("listening on http://{addr}");