Skip to main content

eventsource_client/
lib.rs

1#![warn(rust_2018_idioms)]
2#![allow(clippy::result_large_err)]
3//! Client for the [Server-Sent Events] protocol (aka [EventSource]).
4//!
5//! This library provides SSE protocol support but requires you to bring your own
6//! HTTP transport. See the `examples/` directory for reference implementations using
7//! popular HTTP clients like hyper and reqwest.
8//!
9//! # Getting Started
10//!
11//! ```ignore
12//! use futures::TryStreamExt;
13//! use eventsource_client::{Client, ClientBuilder, SSE};
14//!
15//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
16//! // You need to implement HttpTransport trait for your HTTP client
17//! # struct MyTransport;
18//! # impl launchdarkly_sdk_transport::HttpTransport for MyTransport {
19//! #     fn request(&self, _req: http::Request<Option<String>>) -> eventsource_client::ResponseFuture {
20//! #         unimplemented!()
21//! #     }
22//! # }
23//! let transport = MyTransport::new();
24//!
25//! let client = ClientBuilder::for_url("https://example.com/stream")?
26//!     .header("Authorization", "Bearer token")?
27//!     .build_with_transport(transport);
28//!
29//! let mut stream = client.stream();
30//!
31//! while let Some(event) = stream.try_next().await? {
32//!     match event {
33//!         SSE::Event(evt) => println!("Event: {}", evt.event_type),
34//!         SSE::Comment(comment) => println!("Comment: {}", comment),
35//!         SSE::Connected(_) => println!("Connected!"),
36//!     }
37//! }
38//! # Ok(())
39//! # }
40//! ```
41//!
42//! # Implementing a Transport
43//!
44//! See the [`transport`] module documentation for details on implementing
45//! the [`HttpTransport`] trait.
46//!
47//! [`HttpTransport`]: HttpTransport
48//!
49//! [Server-Sent Events]: https://html.spec.whatwg.org/multipage/server-sent-events.html
50//! [EventSource]: https://developer.mozilla.org/en-US/docs/Web/API/EventSource
51
52mod client;
53mod config;
54mod error;
55mod event_parser;
56mod response;
57mod retry;
58
59pub use client::*;
60pub use config::*;
61pub use error::*;
62pub use event_parser::Event;
63pub use event_parser::SSE;
64pub use response::Response;