simple-actors
Simple message based actors
The actors send messages to each other via a message bus, and they can listen for message patterns.
Install
Install the module via npm:
npm install simple-actors
(Please let me know if you know a better name for this library...)
Use
Example usage:
var actors = ; var bus = ; actor1 = 'actor1'; actor2 = 'actor2'; actor1;actor2; // actor1 listens for messages containing 'hi' or 'hello' (case insensitive)actor1; // actor2 listens for any messageactor2; // send a message to actor 1actor2;
Babble
simple-actors can be used together with babble, extending the actors with support for dynamic communication flows.
Example usage:
var actors = ;var babble = ; // create two actors and babblify themvar emma = babble;var jack = babble; // create a message bus and connect both actorsvar bus = ;emma;jack; emma ; jack ; { console; return message;}
API
The library contains the following prototypes:
actors.Actor
actors.MessageBus
(abstract prototype)actors.LocalMessageBus
using a local, in process message bus.actors.DistribusMessageBus
using distribus.actors.PubNubMessageBus
using PubNub.actors.AMQPMessageBus
using the AMPQ protocol, for example via RabbitMQ servers.
Actor
Constructor:
var actor = id: String;
Methods:
Actor.send(to: String, message: String)
Send a message to an other actor.Actor.onMessage(from: String, message: String)
Receive a message from an actor. The default implementation of this function iterates over all message listeners registered viaActor.on
. The method can be overloaded if needed.Actor.on(pattern: String | RegExp | Function, callback: Function)
Register an message listener, which is triggered when a message comes in which matches given pattern. The pattern can be a String (exact match), a regular expression, or a test function which is invoked aspattern(message)
and must return true or false.Actor.off(pattern: String | RegExp | Function, callback: Function)
Unregister a registered message listener.Actor.connect(messagebus: MessageBus) : Promise<Actor, Error>
Connect the actor to a message bus. The library comes with multiple message bus implementations (see API. An actor can be connected to multiple message buses.Actor.disconnect(messagebus: MessageBus)
Disconnect the actor from a message bus.
MessageBus
The library provides multiple MessageBus
implementations: LocalMessageBus
,
PubNubMessageBus
, AMQPMessageBus
, and DistribusMessageBus
. It is quite
easy to implement a message bus for other messaging services.
MessageBus
has the following API:
Constructor:
var bus = config: Object;
Methods:
MessageBus.connect(id: String, onMessage: Function [, onConnect: Function])
Connect a peer with givenid
. When a message for the peer comes in, the callback functiononMessage
is invoked asonMessage(from: String, message: String)
. The method returns a Promise which resolves when the connection is created.MessageBus.disconnect(id: String)
Disconnect a peer with givenid
.MessageBus.send(from: String, to: String, message: String)
Send a message via the message bus.
Test
First install the project dependencies:
npm install
Then run the tests:
npm test
To do
- Implement a mixin pattern to turn existing objects into an actor.
- Maybe change the API to Promise based.