GithubHelp home page GithubHelp logo

tempbottle / actix-broker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chris-ricketts/actix-broker

0.0 2.0 0.0 37 KB

Message broker for the Actix actor framework

License: Apache License 2.0

Rust 100.00%

actix-broker's Introduction

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

Usage

Simply include the BrokerSubscribe & BrokerIssue traits then the following functions can be used:

// Asynchronously subscribe to a message
self.subscribe_async::<MessageType>(ctx);

// 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::<MessageType>(ctx);

// Synchronously send a message to any subscribers.
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();
    });
}

actix-broker's People

Contributors

chris-ricketts avatar najamelan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.