#encode-decode #server-sent-events #sse #message #protocols #lib

clia-sse-msg-codec

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

1 unstable release

0.1.0 Mar 22, 2022

#22 in #server-sent-events

BSD-2-Clause

5KB
73 lines

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()),
});

lib.rs:

Encode and Decode SSE Messages

No runtime deps