Skip to content
JeanHuguesRobert edited this page Feb 15, 2013 · 3 revisions

Actors and proxies

Actors are objects that communicate the one with the others using messages.

When idle, an actor simply waits for incoming messages. When a message is received, the actor can either decide to process it or decide to process it later, after some other messages. As a result, each actor has a message queue, called a "mailbox" in the actor jargon.

Some messages don't require an answer. They are "send type" messages. Some messages do require an answer. They are "call type" messages.

When it processes a "call type" message, the actor can decide to provide either a direct response or a promise to be fullfilled (or rejected) later on. Until that decision is taken, additional "call type" messages are queued. This makes it easy to "serialize" the processing of calls when that makes sense.

Each actor has a unique name, provided by the actor creator. All actors are remembered in a global registry where one can lookup for them. Names are in the form xx.xx.xx where the last xx is generated by the actor itself when only xx.xx. is provided at creation time (ie when name ends with a dot).

Actors can be usefull to build distributed systems. In these configurations, each javascript process hosts actors and communicate with actors hosted in some other processes, via proxies.

API:

  .actor( name, pattern ) -- start an actor or return an actor generator
  .actor( name )          -- look for an existing actor
    .tell( ... )          -- send a message to the actor
    .ask( ... )           -- send a message and expect an answer
    .receive( pattern )   -- actor redefines reaction to received messages
  .ego                    -- actor the current task is running
  .ego.stage              -- stage the actor received current message from
  .stage( name, [url] )   -- a place with actors in it
  .stage( "local", srv )  -- define http server for local stage
  .proxy( name, url )     -- access to remote actors
  .proxy( name, stage )   -- access to browser side remote actors
    .tell( ... )
    .ask( ... )

Next chapter: Node.js API. Index.