#websocket #network #stream #async

nightly lightws

Lightweight websocket implement for stream transmission

18 releases (5 breaking)

Uses new Rust 2021

0.6.5 Jun 18, 2022
0.6.4 May 7, 2022
0.6.3 Apr 21, 2022
0.5.1 Apr 12, 2022
0.1.0 Mar 17, 2022

#19 in WebSocket

Download history 17/week @ 2022-03-14 14/week @ 2022-03-21 84/week @ 2022-03-28 208/week @ 2022-04-04 701/week @ 2022-04-11 500/week @ 2022-04-18 554/week @ 2022-04-25 342/week @ 2022-05-02 154/week @ 2022-05-09 591/week @ 2022-05-16 217/week @ 2022-05-23 161/week @ 2022-05-30 77/week @ 2022-06-06 431/week @ 2022-06-13 102/week @ 2022-06-20 72/week @ 2022-06-27

769 downloads per month
Used in 3 crates (via kaminari)

MIT license

145KB
3.5K SLoC

Lightws

Lightws Lightws Released API docs crates.io

Lightweight websocket implement for stream transmission.

Features

  • Avoid heap allocation.
  • Avoid buffering frame payload.
  • Use vectored-io if available.
  • Transparent Read/Write over the underlying IO source.

High-level API

[role, endpoint, stream]

Std:

{
    // handshake
    let stream = Endpoint<TcpStream, Client>::connect(tcp, buf, host, path)?;
    // read some data
    stream.read(&mut buf)?;
    // write some data
    stream.write(&buf)?;
}

Async:

{
    // handshake
    let stream = Endpoint<TcpStream, Client>::connect_async(tcp, buf, host, path).await?;
    // read some data
    stream.read(&mut buf).await?;
    // write some data
    stream.write(&buf).await?;
}

Low-level API

[frame, handshake]

Frame:

{
    // encode a frame head
    let head = FrameHead::new(...);
    let offset = unsafe {
        head.encode_unchecked(&mut buf);
    };

    // decode a frame head
    let (head, offset) = FrameHead::decode(&buf).unwrap();
}

Handshake:

{
    // make a client handshake request
    let request = Request::new(b"/ws", b"example.com", "sec-key..");
    let offset = request.encode(&mut buf).unwrap();

    // parse a server handshake response
    let mut custom_headers = HttpHeader::new_storage();
    let mut response = Response::new_storage(&mut custom_headers);
    let offset = response.decode(&buf).unwrap();
}

Dependencies

~0.8–1.4MB
~26K SLoC