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
fully implements standard SSE behaviors, including automatic network
reconnections, exponential backoff, and tracking the Last-Event-ID to resume
dropped streams without missing a beat.
Features
- Ergonomic API: Use the
.into_event_source()extension trait to turn anyreqwest::RequestBuilderinto a stream of events instantly. - Auto-Reconnection: Automatically handles dropped network connections, and timeouts.
- Smart Backoff: Implements exponential backoff with jitter to respect
server load, automatically adapting to
retrydelays requested by the server. - Standards Compliant: Automatically handles
text/event-streamvalidation and attachesLast-Event-IDheaders upon reconnection.
Usage
Add this to your Cargo.toml:
[]
= "0.1"
= { = "0.12", = ["stream"] }
= { = "1", = ["macros", "rt-multi-thread"] }
= "0.3"
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 or custom backoff strategies, use the
EventSourceBuilder:
use ;
use ;
let client = new;
let req = client.get;
let retry_config = SseRetryConfig ;
let mut stream = req.into_event_source_builder
.retry_config
.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?;