3 releases

0.1.2 Apr 7, 2024
0.1.1 Jan 2, 2024
0.1.0 Dec 5, 2021

#24 in #trillium

Download history 1/week @ 2024-05-30 1/week @ 2024-07-25

125 downloads per month

MIT/Apache

68KB
1K SLoC

Welcome to Trillium!

📖 Guide 📖

The guide provides an architectural overview and lay of the land connecting the trillium crates.

📑 Rustdocs 📑

The rustdocs represent the best way to learn about any of trillium's individual crates and the specific interfaces.




Legal:

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

Trillium tools for server sent events

This primarily provides SseConnExt, an extension trait for trillium::Conn that has a with_sse_stream chainable method that takes a Stream where the Item implements Eventable.

Often, you will want this stream to be something like a channel, but the specifics of that are dependent on the event fanout characteristics of your application.

This crate implements Eventable for an Event type that you can use in your application, for String, and for &'static str. You can also implement Eventable for any type in your application.

Example usage

use broadcaster::BroadcastChannel;
use trillium::{conn_try, conn_unwrap, log_error, Conn, Method, State};
use trillium_sse::SseConnExt;
use trillium_static_compiled::static_compiled;

type Channel = BroadcastChannel<String>;

fn get_sse(mut conn: Conn) -> Conn {
let broadcaster = conn_unwrap!(conn.take_state::<Channel>(), conn);
conn.with_sse_stream(broadcaster)
}

async fn post_broadcast(mut conn: Conn) -> Conn {
let broadcaster = conn_unwrap!(conn.take_state::<Channel>(), conn);
let body = conn_try!(conn.request_body_string().await, conn);
log_error!(broadcaster.send(&body).await);
conn.ok("sent")
}

fn main() {
let handler = (
static_compiled!("examples/static").with_index_file("index.html"),
State::new(Channel::new()),
|conn: Conn| async move {
match (conn.method(), conn.path()) {
(Method::Get, "/sse") => get_sse(conn),
(Method::Post, "/broadcast") => post_broadcast(conn).await,
_ => conn,
}
},
);

// trillium_smol::run(handler);
}

Dependencies

~7.5MB
~187K SLoC