expression

Axum SSE: set a reconnection interval

The retry field suggests a reconnection delay in milliseconds. It does not keep a connection alive or impose a hard deadline; clients may apply additional backoff. Standalone synthetic example. Expected stdout (not executed): retry: 3000 ms Common mistake: Confusing milliseconds with seconds or treating the retry interval as an HTTP timeout.

Formal Syntax & Structure

rust syntaxrust
Event::default().retry(Duration::from_secs(3))

Working Example

Example Snippetrust
use axum::{Router, body::{Body, to_bytes}, http::{Request, StatusCode}, response::{IntoResponse, sse::{Event, Sse}}, extract::ws::{Message, WebSocketUpgrade, WebSocket}};use std::convert::Infallible;use futures_util::stream;use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() {    let event = Event::default().retry(std::time::Duration::from_secs(3)).data("ready");    let res = Sse::new(stream::iter([Ok::<_, Infallible>(event)])).into_response();    let bytes = to_bytes(res.into_body(), 1024).await.unwrap();    let text = std::str::from_utf8(&bytes).unwrap();    let delay = text.lines().find_map(|s| s.strip_prefix("retry:")).unwrap().trim_start();    assert_eq!(delay.parse::<u64>().unwrap(), 3000);    println!("retry: {delay} ms");}
Rust Reference