clia-sse-msg-codec

Crates.ioclia-sse-msg-codec
lib.rsclia-sse-msg-codec
version0.1.0
sourcesrc
created_at2022-03-22 06:40:19.575987
updated_at2022-03-22 06:40:19.575987
descriptionA Rust lib to encode and decode the SSE (Server-Sent Events) protocol message.
homepage
repositoryhttps://github.com/clia/sse-msg-codec
max_upload_size
id554514
size6,302
clia (clia)

documentation

README

sse-msg-codec

A Rust lib to encode and decode the SSE (Server-Sent Events) protocol message.

Protocol

The SSE (Server-Sent Events) Protocol, See: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format

Usage

Encode:

let encoded = sse_msg_codec::encode(
    Some("test-id"), 
    Some("test-event"), 
    Some("test-data line1\nline2"),
);
assert_eq!(
    encoded, 
    "id: test-id\nevent: test-event\ndata: test-data line1\ndata: line2\n\n".to_owned(),
);

Decode:

let msg = "id: test-id\nevent: test-event\ndata: test-data line1\ndata: line2\n\n";
let decoded = sse_msg_codec::decode(msg);
assert_eq!(decoded, SseMsg {
    id: Some("test-id".to_owned()),
    event: Some("test-event".to_owned()),
    data: Some("test-data line1\nline2".to_owned()),
});
Commit count: 9

cargo fmt