#channel #io-stream #stream #async-io #networking #server #client

channels

Bidirectional channel-like communication over generic Read/Write streams

22 releases (10 breaking)

0.11.2 Feb 18, 2024
0.11.0 Dec 29, 2023
0.10.0 Aug 30, 2023
0.8.0 Jun 18, 2023
0.1.1 Jul 23, 2022

#419 in Network programming

Download history 1/week @ 2023-12-28 2/week @ 2024-02-01 131/week @ 2024-02-15 37/week @ 2024-02-22 6/week @ 2024-02-29 1/week @ 2024-03-07 1/week @ 2024-03-14

58 downloads per month

MIT license

85KB
2K SLoC

logo

Easy and fast communication between processes, threads and systems.

license-badge tests-badge version-badge docs-badge downloads-badge


Sender/Receiver types for communicating with a channel-like API across generic IO streams. It takes the burden on serializing, deserializing and transporting data off your back and let's you focus on the important logic of your project. It is:

  • Fast: The simple protocol allows low-overhead transporting of data.

  • Modular: Channels' sans-io approach means it can be used on top of any medium, be it a network socket, a pipe, a shared memory region, a file, anything.

  • Ergonomic: The API offered empowers you to use your time on building the logic of your application instead of worrying about data transport.

  • Async & sync first: Channels natively supports both synchronous and asynchronous operation with no hacky workarounds like spawning threads or running a separate runtime.

In action

[dependencies.channels]
version = "0.11.0"
features = ["full", "tokio"]
use tokio::net::TcpStream;

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
enum Message {
    Ping,
    Pong
}

#[tokio::main]
async fn main() {
    let stream = TcpStream::connect("127.0.0.1:8080").await.unwrap();
    let (r, w) = stream.into_split();
    let (mut tx, mut rx) = channels::channel::<Message, _, _>(r, w);

    loop {
        match rx.recv().await.unwrap() {
            Message::Ping => {
                println!("pinged!");
                tx.send(Message::Pong).await.unwrap();
            }
            Message::Pong => {
                println!("ponged!");
            }
        }
    }
}

For more, see: examples/

How it works

Channels implements a communication protocol that allows sending and receiving data across any medium. It works over any stream synchronous or asynchronous. Currently it can work with any of the following IO traits:

You can find out more about how the underlying communication protocol works here.

License

Dependencies

~0–1.6MB
~30K SLoC