#actix #message #broker

actix-broker

Message broker for the Actix actor framework

13 releases

0.3.1 Nov 5, 2020
0.3.0 Jan 15, 2020
0.2.1 Oct 27, 2019
0.2.0 Jun 28, 2019
0.1.6 Sep 24, 2018

#143 in HTTP server

Download history 102/week @ 2020-08-13 196/week @ 2020-08-20 176/week @ 2020-08-27 250/week @ 2020-09-03 395/week @ 2020-09-10 110/week @ 2020-09-17 142/week @ 2020-09-24 180/week @ 2020-10-01 110/week @ 2020-10-08 156/week @ 2020-10-15 109/week @ 2020-10-22 123/week @ 2020-10-29 171/week @ 2020-11-05 177/week @ 2020-11-12 143/week @ 2020-11-19 156/week @ 2020-11-26

726 downloads per month

MIT/Apache

22KB
293 lines

Actix-Broker

crates.io

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


lib.rs:

Actix-Broker

The actix_broker crate contains SystemService and ArbiterService Brokers that keep track of active subscriptions to different Messages. Broker services are automatically started when an actor uses functions from the BrokerSubscribe and BrokerIssue traits to either subscribe to or issue a message.

Example

# #[macro_use]
# extern crate actix;
# extern crate actix_broker;
use actix::prelude::*;
use actix_broker::{BrokerSubscribe, BrokerIssue, SystemBroker, ArbiterBroker, Broker};

// Note: The message must implement 'Clone'
#[derive(Clone, Message)]
#[rtype(result = "()")]
struct MessageOne;

#[derive(Clone, Message)]
#[rtype(result = "()")]
struct MessageTwo;

#[derive(Clone, Message)]
#[rtype(result = "()")]
struct MessageThree;

struct ActorOne;

impl Actor for ActorOne {
    // Note: The actor context must be Asynchronous,
    // i.e. it cannot be 'SyncContext'
    type Context = Context<Self>;

    fn started(&mut self,ctx: &mut Self::Context) {
        // Asynchronously subscribe to a message on the system (global) broker
        self.subscribe_system_async::<MessageOne>(ctx);
        // Asynchronously issue a message to any subscribers on the system (global) broker
        self.issue_system_async(MessageOne);
        // Synchronously subscribe to a message on the arbiter (local) broker
        self.subscribe_arbiter_sync::<MessageTwo>(ctx);
        // Synchronously issue a message to any subscribers on the arbiter (local) broker
        self.issue_arbiter_sync(MessageTwo, ctx);
    }
}
    
    // To subscribe to a messsage, the actor must handle it
impl Handler<MessageOne> for ActorOne {
    type Result = ();

    fn handle(&mut self, msg: MessageOne, ctx: &mut Self::Context) {
        // An actor does not have to handle a message to just issue it
        self.issue_async::<SystemBroker, _>(MessageThree);
    }
}

// Messages can also be sent from outside actors
fn my_function() {
    Broker::<SystemBroker>::issue_async(MessageOne);
}
#
# // Handler for MessageTwo...
# impl Handler<MessageTwo> for ActorOne {
#     type Result = ();

#     fn handle(&mut self, msg: MessageTwo, ctx: &mut Self::Context) {
#     }
# }
# fn main() {}

Dependencies

~5.5MB
~104K SLoC