Crates.io | taps |
lib.rs | taps |
version | 0.2.2 |
source | src |
created_at | 2023-10-15 03:53:20.286403 |
updated_at | 2023-10-15 05:06:55.806998 |
description | taps (Tokio Asynchronous Pub/Sub) is an in-process async message broker that can be used for messaging between spawned tokio tasks. |
homepage | https://github.com/matthewjberger/taps |
repository | https://github.com/matthewjberger/taps |
max_upload_size | |
id | 1003467 |
size | 41,253 |
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.
Add the following to your Cargo.toml
:
[dependencies]
taps = "0.2.2"
tokio = { version = "1.33.0", features = ["full"] }
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!"
}
}