2 releases

0.1.1 Sep 18, 2021
0.1.0 Sep 18, 2021

#627 in HTTP server

Download history 2705/week @ 2024-07-19 3396/week @ 2024-07-26 5456/week @ 2024-08-02 2167/week @ 2024-08-09 2137/week @ 2024-08-16 1180/week @ 2024-08-23 1782/week @ 2024-08-30 1560/week @ 2024-09-06 1740/week @ 2024-09-13 1552/week @ 2024-09-20 1722/week @ 2024-09-27 1830/week @ 2024-10-04 1879/week @ 2024-10-11 2439/week @ 2024-10-18 3377/week @ 2024-10-25 1776/week @ 2024-11-01

9,957 downloads per month

MIT license

21KB
364 lines

Mousse

An SSE encoder/decoder

Usage

Decode

use futures::stream::StreamExt;
use mousse::Parser;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let url = match std::env::args().nth(1) {
        Some(url) => url,
        None => {
            eprintln!("expected uri argument\ncargo run -- <uri>");
            std::process::exit(1);
        }
    };
    let mut stream = reqwest::get(&url)
        .await?
        .bytes_stream();
    while let Some(chunk) = stream.next().await {
        let chunk = chunk?;
        let string = String::from_utf8_lossy(&chunk);
        let mut parser = Parser::new(&string);
        println!("{:?}", parser.next_event());
    }
    Ok(())
}

Output

If you connect this to an sse server, you might see something like this.

cargo run --example reqwest -- http://localhost:8080/sse
Some(ServerSentEvent { comment: None, event: None, id: None, data: Some("next tick in 5"), retry: None })
Some(ServerSentEvent { comment: Some(""), event: None, id: None, data: None, retry: None })
Some(ServerSentEvent { comment: None, event: None, id: None, data: Some("next tick in 4"), retry: None })
Some(ServerSentEvent { comment: Some(""), event: None, id: None, data: None, retry: None })
Some(ServerSentEvent { comment: None, event: None, id: None, data: Some("next tick in 2"), retry: None })
Some(ServerSentEvent { comment: None, event: None, id: None, data: Some("next tick in 1"), retry: None })

Encode

use mousse::ServerSentEvent;

fn main() {
    for i in 0..10 {
        println!(
            "{}",
            ServerSentEvent::builder()
                .data(&format!("{}: Hi I am a data field", i))
                .id(&i.to_string())
                .build()
        )
    }
}

Output

cargo run --example encode
id:0
data:0: Hi I am a data field


id:1
data:1: Hi I am a data field


id:2
data:2: Hi I am a data field


id:3
data:3: Hi I am a data field


id:4
data:4: Hi I am a data field


id:5
data:5: Hi I am a data field


id:6
data:6: Hi I am a data field


id:7
data:7: Hi I am a data field


id:8
data:8: Hi I am a data field


id:9
data:9: Hi I am a data field


Dependencies

~88KB