diff --git a/backend/src/db/mod.rs b/backend/src/db/mod.rs index a8cc11d1f..54b849610 100644 --- a/backend/src/db/mod.rs +++ b/backend/src/db/mod.rs @@ -14,6 +14,7 @@ use rpc_toolkit::hyper::{Body, Error as HyperError, Request, Response}; use rpc_toolkit::yajrc::RpcError; use serde::{Deserialize, Serialize}; use serde_json::Value; +use tokio::io::AsyncWrite; use tokio::sync::oneshot; use tokio::task::JoinError; use tokio_tungstenite::tungstenite::protocol::frame::coding::CloseCode; @@ -82,6 +83,7 @@ async fn deal_with_messages( mut sub: patch_db::Subscriber, mut stream: WebSocketStream, ) -> Result<(), Error> { + let mut timer = tokio::time::interval(tokio::time::Duration::from_secs(5)); loop { futures::select! { _ = (&mut kill).fuse() => { @@ -104,14 +106,19 @@ async fn deal_with_messages( } message = stream.next().fuse() => { let message = message.transpose().with_kind(crate::ErrorKind::Network)?; - match message { - None => { - tracing::info!("Closing WebSocket: Stream Finished"); - return Ok(()) - } - _ => (), + if message.is_none() { + tracing::info!("Closing WebSocket: Stream Finished"); + return Ok(()) } } + // This is trying to give a health checks to the home to keep the ui alive. + _ = timer.tick().fuse() => { + stream + .send(Message::Ping(vec![])) + .await + .with_kind(crate::ErrorKind::Network)?; + } + } } }