#stream #async #networking #heap-allocation #io-stream

nightly lightws

Lightweight websocket implement for stream transmission

25 releases

0.6.12 Oct 14, 2024
0.6.10 Dec 9, 2023
0.6.8 Apr 27, 2023
0.6.7 Aug 30, 2022
0.2.0 Mar 27, 2022

#45 in WebSocket

Download history 13/week @ 2024-08-05 7/week @ 2024-08-12 9/week @ 2024-08-19 112/week @ 2024-08-26 169/week @ 2024-09-02 8/week @ 2024-09-09 595/week @ 2024-09-16 397/week @ 2024-09-23 304/week @ 2024-09-30 160/week @ 2024-10-07 333/week @ 2024-10-14 73/week @ 2024-10-21 8/week @ 2024-10-28 20/week @ 2024-11-04 24/week @ 2024-11-11 206/week @ 2024-11-18

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

MIT license

150KB
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.9–6.5MB
~40K SLoC