1.0.11 • Published 8 years ago

actorjs-core v1.0.11

Weekly downloads
40
License
-
Repository
github
Last release
8 years ago

ActorJs Core

ActorJs is a basic implementation of an actor model. The implementation is heavily based on the Akka.

ActorJs works with nodejs as well in the browser. This makes that it easy integrate client and server.

Javascript is single threaded language. So why does it make sens to write an actor model in this language. Things like thread polling are not applicable for this language. Although there are advantages of an actor system which can be use full in javascript.

To make your application vertical and horizontal scalable actor model can be very useful. By making use of remoting different of instances of the application can pass messages.

Event sourcing is another advantage which can be leveraged from the actor model. By persisting events and replay them to get back the state.

Installation

Install it

npm i actorjs-core

Use it in nodejs

var actorJsCore = require('actor-core');

Use it in web

var actorJsCore =  actorjs.core

Actors

Actors are defined as an object or a function. The actor only requires a single receive function.

Actor as object

var MyActor = {
    receive: function(msg){
        console.log(msg)
    }
}

Actor as function

function MyActor () {
    this.receive = function(msg){
        console.log(msg)
    }
}

An actor is instantiate by making use of the actorOf function. This function can be called on the system or on an other actor. By using the function on a actor hierarchy of actors can be created.

var system = new ActorSystem('MySystem');
var actorRef = system.actorOf(MyActor);
var ref = actorRef.actorOf(MyActor);

Self

With self a message can be send to the actor its own inbox.

function actor () {
 
  this.receive = function(msg){
    this.context.self.tell("Hello Parent!")
  }
} 

Parent

With parent a message can be send to the overlying actor in the system.

function actor () {
  
  this.receive = function(msg){
    this.context.parent.tell("Hello Parent!")
  }
} 

Sender

Sender gives the ability to reply a message to the sending actor. To be able to send back a message to the actor a reference is needed to the sending actorRef. To provide this actorRef an reference to the actor needs to be provided will sending a message. This is done by passing it to the second argument of the tell function. The following example will end up in a endless loop but to get the idea.

function SendActor(actorRef) {
    this.receive = function (message) {
        actorRef.tell("Hello World!", this);
    }
}

function ActorReply() {
    this.receive = function (message) {
        this.sender.tell("Hello Back!")
    }
}

var actorReplyRef = system.actorOf(new ActorReply());
var actorSendRef = system.actorOf(new ActorSend(actorReplyRef))

Forward

Forwarding messages sends a message to the next actor but keeps the original sender. This is done by giving te sender as the second argument of the tell function. Example also end up in endless loop.

function SendActor(forwardActorRef) {
    this.receive = function (message) {
        forwardActorRef.tell("Hello World!", this);
    }
}

function ForwardActor(replyActorRef) {
    this.receive = function (message) {
        replyActorRef.tell(message, this.sender)
    }
}

function ReplyActor() {
    this.sender.tell("Hello Back!")
}

var system = new ActorSystem('my-sys');

var replyActorRef = system.actorOf(new ReplyActor());
var forwardActorRef = system.actorOf(new ForwardActor(replyActorRef));
var sendActorRef = system.actorOf(new SendActor(forwardActorRef))

Messages

Messages are helper classes to generate messages in a predefined way. These messages are understood by the corresponding matchers.

Key Value Message

Key Value Messages are messages which hold one object with one key value pair. The KeyValueMacher is able to match on the these messages.

ActorMessages.KeyValueMessage(<key>, <value>);

This produces the following message

{ <key>: <value> }

Matchers

Matchers are introduced to easy match on incoming messages. There are different matchers which can be used.

Key Value Matcher

Key Value Matcher assumes that the message which is send has the following structure.

{ <key>: <value> }

Key Value Matcher can be used as follow

ActorMatcher.KeyValueMatcher({
    <key>: function(<value>){
        // do something wtith <value>
    }
);
1.0.11

8 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago