A crate that adds a message broker to the Actix actor framework.
Subscribe to and issue Actix Messages easily in an asynchronous or synchronous manner.
Simply include the BrokerSubscribe
& BrokerIssue
traits then the following functions can be used:
```rust
// Asynchronously subscribe to a message
self.subscribe_async::
// Asynchronoulsy send a message to any subscribers let msg = MessageType::new(); self.issue_async(msg);
// Synchronously subscribe to a message
// The calling actor will be notified of the
// last message issued to the broker for that MessageType
self.subscribe_sync::
// Synchronously send a message to any subscribers. let msg = MessageType::new(); self.issue_sync(msg, ctx); ```
```rust
extern crate actix; extern crate actix_broker;
use actix::prelude::*; use actix_broker::{BrokerSubscribe, BrokerIssue};
struct ActorOne; struct ActorTwo; struct ActorThree;
impl Actor for ActorOne {
type Context = Context
fn started(&mut self, ctx: &mut Self::Context) {
self.subscribe_sync::<MessageTwo>(ctx);
self.issue_async(MessageOne("hello".to_string()));
}
}
impl Handler
fn handle(&mut self, msg: MessageTwo, ctx: &mut Self::Context) {
println!("ActorOne Received: {:?}", msg);
}
}
impl Actor for ActorTwo {
type Context = Context
fn started(&mut self, ctx: &mut Self::Context) {
self.subscribe_async::<MessageOne>(ctx);
}
}
impl Handler
fn handle(&mut self, msg: MessageOne, ctx: &mut Self::Context) {
println!("ActorTwo Received: {:?}", msg);
self.issue_async(MessageTwo(0));
}
}
impl Actor for ActorThree {
type Context = Context
fn started(&mut self, ctx: &mut Self::Context) {
self.subscribe_async::<MessageOne>(ctx);
}
}
impl Handler
fn handle(&mut self, msg: MessageOne, ctx: &mut Self::Context) {
println!("ActorThree Received: {:?}", msg);
self.issue_async(MessageTwo(1));
}
}
struct MessageOne(String);
struct MessageTwo(u8);
fn main() { System::run(|| { ActorTwo.start(); ActorThree.start(); ActorOne.start(); }); } ```