Crate actix_broker[][src]

Actix-Broker

The actix_broker crate contains a Broker SystemService that keeps track of active subscriptions to different Messages. This Broker service is automatically started when an actor uses functions from the BrokerSubscribe and BrokerIssue traits to either subscribe to or issue a message.

Example

use actix::prelude::*;
// Bring both these traits into scope.
use actix_broker::{BrokerSubscribe, BrokerIssue};

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

#[derive(Clone, Message)]
struct MessageTwo;

#[derive(Clone, Message)]
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
        self.subscribe_async::<MessageOne>(ctx);
        // Asynchronously issue a message to any subscribers
        self.issue_async(MessageOne);
        // Synchronously subscribe to a message
        self.subscribe_sync::<MessageTwo>(ctx);
        // Synchronously issue a message to any subscribers
        self.issue_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(MessageThree);
    }
}

    // Handler for MessageTwo...

Structs

Broker

Traits

BrokerIssue

The BrokerIssue provides functions to issue messages to any subscribers.

BrokerSubscribe

The BrokerSubscribe trait has functions to register an actor's interest in different messages.