npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-actors

v2.0.0

Published

Simple message based actors.

Downloads

17

Readme

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 = require('simple-actors');

var bus = new actors.LocalMessageBus();
    actor1 = new actors.Actor('actor1');
    actor2 = new actors.Actor('actor2');

actor1.connect(bus);
actor2.connect(bus);

// actor1 listens for messages containing 'hi' or 'hello' (case insensitive)
actor1.on(/hi|hello/i, function (from, message) {
  console.log(from + ' said: ' + message);

  // reply to the greeting
  this.send(from, 'Hi ' + from + ', nice to meet you!');
});

// actor2 listens for any message
actor2.on(/./, function (from, message) {
  console.log(from + ' said: ' + message);
});

// send a message to actor 1
actor2.send('actor1', 'Hello actor1!');

Babble

simple-actors can be used together with babble, extending the actors with support for dynamic communication flows.

Example usage:

var actors = require('simple-actors');
var babble = require('babble');

// create two actors and babblify them
var emma = babble.babblify(new actors.Actor('emma'));
var jack = babble.babblify(new actors.Actor('jack'));

// create a message bus and connect both actors
var bus = new actors.LocalMessageBus();
emma.connect(bus);
jack.connect(bus);

emma.listen('hi')
    .listen(printMessage)
    .decide(function (message, context) {
      return (message.indexOf('age') != -1) ? 'age' : 'name';
    }, {
      'name': babble.tell('hi, my name is emma'),
      'age':  babble.tell('hi, my age is 27')
    });

jack.tell('emma', 'hi')
    .tell(function (message, context) {
      if (Math.random() > 0.5) {
        return 'my name is jack'
      } else {
        return 'my age is 25';
      }
    })
    .listen(printMessage);

function printMessage (message, context) {
  console.log(context.from + ': ' + message);
  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 = new actors.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 via Actor.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 as pattern(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 = new MessageBus([config: Object]);

Methods:

  • MessageBus.connect(id: String, onMessage: Function [, onConnect: Function])
    Connect a peer with given id. When a message for the peer comes in, the callback function onMessage is invoked as onMessage(from: String, message: String). The method returns a Promise which resolves when the connection is created.
  • MessageBus.disconnect(id: String)
    Disconnect a peer with given id.
  • 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.