tokio_sse_codec

Crates.iotokio_sse_codec
lib.rstokio_sse_codec
version0.0.2
sourcesrc
created_at2023-08-12 00:28:53.163085
updated_at2023-08-12 00:34:15.86158
descriptionTokio codec for Server-Sent Events
homepage
repositoryhttps://github.com/tarqd/ldactl/tree/main/tokio_sse_codec
max_upload_size
id942488
size78,757
Christopher Tarquini (tarqd)

documentation

README

Server-Sent Event Streams Codec

Implements a [Codec] for encoding and decoding [Server-Sent Events] streams.

Advantages:

  • Minimizes allocations by using the buffer provided by [FramedWrite] and [FramedRead] while parsing lines
  • Easy to use with the rest of the tokio ecosystem
  • Can be used with any type that implements [AsyncRead] or [AsyncWrite]
  • Errors implement [miette::Diagnostic] for better error and diagnostic messages

Examples

use futures::StreamExt;
use tokio_util::codec::{FramedRead, Decoder};
use tokio_sse_codec::{SseDecoder, Frame, Event, SseDecodeError};

#[tokio::main]
async fn main() -> Result<(), SseDecodeError> {
// you can use any stream or type that implements `AsyncRead`
let data = "id: 1\nevent: example\ndata: hello, world\n\n";
let mut reader = FramedRead::new(data.as_bytes(), SseDecoder::<String>::new());

    while let Some(Ok(frame)) = reader.next().await {
    match frame {
        Frame::Event(event) => println!("event: id={:?}, name={}, data={}", event.id, event.name, event.data),
        Frame::Comment(comment) => println!("comment: {}", comment),
        Frame::Retry(duration) => println!("retry: {:#?}", duration),
    }
    }

    Ok::<(), SseDecodeError>(())
}

Setting a buffer size limit

By default, the decoder will not limit the size of the buffer used to store the data of an event. It's recommended to set one when dealing with untrusted input, otherwise a malicious server could send a very large event and consume all available memory.

The buffer should be able to hold a single event and it's data.

use tokio_sse_codec::SseDecoder;

let decoder  = SseDecoder::<String>::with_max_size(1024);
Commit count: 0

cargo fmt