5 unstable releases

0.3.2 Jul 30, 2020
0.3.1 Mar 4, 2020
0.3.0 Mar 2, 2020
0.2.0 Jan 22, 2020
0.1.0 Dec 23, 2019

#1 in #encodes

Download history 5365/week @ 2024-07-21 4712/week @ 2024-07-28 4326/week @ 2024-08-04 6127/week @ 2024-08-11 5727/week @ 2024-08-18 4652/week @ 2024-08-25 5380/week @ 2024-09-01 4569/week @ 2024-09-08 2735/week @ 2024-09-15 3043/week @ 2024-09-22 4629/week @ 2024-09-29 7926/week @ 2024-10-06 6970/week @ 2024-10-13 5659/week @ 2024-10-20 7959/week @ 2024-10-27 5949/week @ 2024-11-03

26,813 downloads per month
Used in 19 crates (3 directly)

MPL-2.0 license

33KB
757 lines

sse-codec

A futures_codec that encodes and decodes Server-Sent Event/Event Sourcing streams.

Documentation

On docs.rs.

Installation

With cargo-edit do:

cargo add sse-codec

Or in Cargo.toml:

[dependencies]
sse-codec = "0.3.2"
  • surf-sse - EventSource client based on Surf and sse-codec.

License

MPL-2.0


lib.rs:

A futures_codec that encodes and decodes Server-Sent Event/Event Sourcing streams.

It emits or serializes full messages, and the meta-message retry:.

Examples

use sse_codec::{decode_stream, Event};
use futures::stream::TryStreamExt; // for try_next()

let response = surf::get("https://some-site.com/events").await?;
let mut events = decode_stream(response);

while let Some(event) = events.try_next().await? {
    println!("incoming: {:?}", event);

    match event {
        Event::Retry { retry } => {
            // change a retry timer value or something
        }
        Event::Message { event, .. } if event == "stop" => {
            break;
        }
        Event::Message { id, event, data } => {
            if let Some(id) = id {
                // change the last event ID
            }
            // handle event here
        }
    }
}

EventSource tests from the web-platform-tests suite. See https://github.com/web-platform-tests/wpt/tree/master/eventsource

Dependencies

~3MB
~61K SLoC