eventsource-client
Client for the Server-Sent Events protocol (aka EventSource).
This library provides a complete SSE protocol implementation with a built-in HTTP transport powered by hyper v1. The pluggable transport design also allows you to use your own HTTP client (reqwest, custom, etc.) if needed.
Quick Start
1. Add dependencies
[]
= { = "0.17", = ["hyper-rustls-native-roots"] }
= "0.3"
= { = "1", = ["macros", "rt-multi-thread"] }
Features:
hyper- Enables the built-inHyperTransportfor HTTP support (enabled by default)hyper-rustls-native-roots,hyper-rustls-webpki-roots, ornative-tls- Adds HTTPS support via rustls (optional)
2. Use the client
use ;
use HyperTransport;
use TryStreamExt;
use Duration;
async
Example
The tail example demonstrates a complete SSE client using the built-in HyperTransport:
Run with HTTP:
Run with HTTPS:
The example shows:
- Creating a
HyperTransportwith custom timeouts - Building an SSE client with authentication headers
- Configuring automatic reconnection with exponential backoff
- Handling different SSE event types (events, comments, connection status)
- Proper error handling for HTTPS URLs without the
hyper-rustls-native-rootsfeature
See examples/tail.rs for the complete implementation.
Features
- Built-in HTTP transport - Production-ready
HyperTransportpowered by hyper v1 - Configurable timeouts - Connect, read, and write timeout support
- HTTPS support - Optional rustls integration via the
hyper-rustls-*ornative-tlsfeatures - Pluggable transport - Use a custom HTTP client if needed (reqwest, etc.)
- Tokio-based streaming - Efficient async/await support
- Custom headers - Full control over HTTP requests
- Automatic reconnection - Configurable exponential backoff
- Retry logic - Handle transient failures gracefully
- Redirect following - Automatic handling of HTTP redirects
- Last-Event-ID - Resume streams from last received event
Custom HTTP Transport
While the built-in HyperTransport works for most use cases, you can implement the HttpTransport trait to use your own HTTP client:
use ;
use Bytes;
This allows you to:
- Use a different HTTP client (reqwest, custom, etc.)
- Implement custom connection pooling or proxy logic
- Add specialized middleware or observability
Architecture
┌─────────────────────────────────────┐
│ Your Application │
└─────────────┬───────────────────────┘
│
▼
┌─────────────────────────────────────┐
│ eventsource-client │
│ (SSE Protocol Implementation) │
└─────────────┬───────────────────────┘
│ HttpTransport trait
▼
┌─────────────────────────────────────┐
│ HTTP Transport Layer │
│ • HyperTransport (built-in) │
│ • Custom (reqwest, etc.) │
└─────────────────────────────────────┘
Stability
This library is actively maintained. The SSE protocol implementation is stable. Breaking changes follow semantic versioning.