sse-reqwest-client
A robust, auto-reconnecting Server-Sent Events (SSE) client built on top of
reqwest.
This crate provides a high-level, ergonomic API for consuming SSE streams. It
implements standard
WHATWG SSE
behaviors out of the box, including automatic network reconnections, handling
Last-Event-ID tracking, and the recommended exponential backoff.
Features
- Ergonomic API: Use the
.into_event_source()extension trait to turn anyreqwest::RequestBuilderinto a stream of events instantly. - Standards Compliant: Automatically handles
text/event-streamvalidation, strictly adheres to HTTP closure rules, and attachesLast-Event-IDheaders upon reconnection. - Auto-Reconnection: Seamlessly recovers from dropped network connections and clean EOFs without losing your place in the stream.
- Transient Error Recovery: Includes opt-in support for automatically retrying recoverable proxy or server errors (e.g., 502, 503, 504, 429).
- Smart Backoff: Implements exponential backoff with jitter, automatically
adapting to the delays requested by the server via
retryevents.
Usage
Add this to your Cargo.toml:
[]
= "0.1"
Quick Start
The easiest way to start listening to events is via the RequestBuilderExt
trait:
use StreamExt;
use ;
async
Advanced Configuration
If you need to configure payload limits, custom backoff strategies, or enable
transient error retries, use the EventSourceBuilder:
use ;
use ;
let client = new;
let mut stream = client
.get
.into_event_source_builder
.retry_transient_errors // Automatically retry on 502/503/504
.initial_reconnection_time
.max_payload_size // 1MB limit
.build;
Important Note on Timeouts
When configuring your reqwest::Client, do not use
reqwest::ClientBuilder::timeout(). That method enforces a strict time limit
on the entire lifespan of the HTTP request, which will forcefully terminate
your persistent SSE stream.
Instead, to handle dead sockets, use TCP Keepalive:
let client = builder
.tcp_keepalive
.build?;