Actix-Broker
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.
Documentation
Usage
Simply include the BrokerSubscribe
& BrokerIssue
traits then the following functions can be used:
self.subscribe_async::<MessageType>(ctx);
let msg = MessageType::new();
self.issue_async(msg);
self.subscribe_sync::<MessageType>(ctx);
let msg = MessageType::new();
self.issue_sync(msg, ctx);
Example
#[macro_use]
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<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
self.subscribe_sync::<MessageTwo>(ctx);
self.issue_async(MessageOne("hello".to_string()));
}
}
impl Handler<MessageTwo> for ActorOne {
type Result = ();
fn handle(&mut self, msg: MessageTwo, ctx: &mut Self::Context) {
println!("ActorOne Received: {:?}", msg);
}
}
impl Actor for ActorTwo {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
self.subscribe_async::<MessageOne>(ctx);
}
}
impl Handler<MessageOne> for ActorTwo {
type Result = ();
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<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
self.subscribe_async::<MessageOne>(ctx);
}
}
impl Handler<MessageOne> for ActorThree {
type Result = ();
fn handle(&mut self, msg: MessageOne, ctx: &mut Self::Context) {
println!("ActorThree Received: {:?}", msg);
self.issue_async(MessageTwo(1));
}
}
#[derive(Clone, Debug, Message)]
struct MessageOne(String);
#[derive(Clone, Debug, Message)]
struct MessageTwo(u8);
fn main() {
System::run(|| {
ActorTwo.start();
ActorThree.start();
ActorOne.start();
});
}