No description
Find a file
2022-11-11 14:43:51 +01:00
.config Use a unit test reporter 2022-11-11 12:14:28 +01:00
.github/workflows Use a specialized caching mechanism 2022-11-11 12:50:17 +01:00
src Initial import 2022-11-10 21:38:45 +01:00
tests Initial import 2022-11-10 21:38:45 +01:00
.gitignore Initial import 2022-11-10 21:38:45 +01:00
Cargo.toml Add some metadata to this crate 2022-11-11 13:34:50 +01:00
LICENSE Initial commit 2022-11-10 21:38:38 +01:00
README.md Add some badges 2022-11-11 14:43:51 +01:00

tokio-messaging

repository build crate license

A crate which offers non-blocking publish/subscribe functionality using Tokio channels.

Publishing messages and subscribing to them is done using an Messaging instance, which acts as a message broker.

In order to create a message broker, start by defining the structure of messages and their data (payload). The types should implement Message and MessageData respectively.

enum MyMessage
{
	Greeting,
	Request
}
 
impl Message for MyMessage {}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct MyPayload(&'static str);
 
impl MessageData for MyPayload {}

Next, create the message broker instance. Usually, you'll have a single, long-living instance.

lazy_static! {
	static ref INSTANCE: Messaging<MyMessage, MyPayload> = { Messaging::new() };
}

pub fn messaging() -> &'static Messaging<MyMessage, MyPayload> { &INSTANCE }

Publish messages using the dispatch() function and subscribe to them using the on() function.

// Subscribe to messages
tokio::spawn(messaging().on(MyMessage::Request, |data: MyPayload| {
	assert_eq!(data.0, "Here's a request!");
}));

// Publish a message
messaging().dispatch(MyMessage::Request, MyPayload("Here's a request!"));