#tokio #message-broker #messaging #async

taps

taps (Tokio Asynchronous Pub/Sub) is an in-process async message broker that can be used for messaging between spawned tokio tasks

4 releases

0.2.2 Oct 15, 2023
0.2.1 Oct 15, 2023
0.2.0 Oct 15, 2023
0.1.0 Oct 15, 2023

#401 in Asynchronous

34 downloads per month

MIT license

16KB
224 lines

Taps - (Tokio Async Pub/Sub)

github crates.io docs.rs

taps is an in-process async message broker built on the tokio runtime that allows multiple clients to communicate with each other through a central broker, routing messages based on topics.

Features

  • Topic-based Routing: Easily send and receive messages based on topics.
  • Asynchronous: Built for asynchronous environments, utilizing the power of Tokio.
  • Scalable: Designed to handle multiple clients communicating simultaneously.

Quick Start

Installation

Add the following to your Cargo.toml:

[dependencies]
taps = "0.2.2"
tokio = { version = "1.33.0", features = ["full"] }

Usage

use taps::{Broker, Client};

#[tokio::main]
async fn main() {
    let mut broker = Broker::new();
    let (worker_tx, worker_rx) = tokio::sync::mpsc::channel(32);
    tokio::spawn(async move {
        broker.run(worker_rx).await;
    });

    let mut client1 = Client::new(worker_tx.clone());
    client1.subscribe("topic1".to_string()).await;

    let mut client2 = Client::new(worker_tx.clone());
    client2.subscribe("topic1".to_string()).await;

    client1
        .publish("topic1".to_string(), "Hello from client1!".to_string())
        .await;
    if let Some(msg_from_client2) = client2.receive("topic1").await {
        println!("{msg_from_client2}"); // Outputs: "Hello from client1!"
    }
}

Dependencies

~2.4–8.5MB
~57K SLoC