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

akkajs

v1.0.1

Published

Akka Actor Framework for JS.

Downloads

19

Readme

Akka.Js

NPM Build Status

This project aim to bring Akka framework available for JS applications.

NOTE the current exposed API is minimal, I will be happy to receive Issues asking for further functionalities to be exposed, and more happy to have PR.

Quick start

Install the module:

npm install akkajs

A simple Ping Pong between two actors looks like:

const akkajs = require("akkajs")

const system = akkajs.ActorSystem.create()

class Pinger extends akkajs.Actor {
  constructor() {
    super()
    this.name = "pinger"
    this.receive = this.receive.bind(this)
  }
  receive(msg) {
    console.log("RECEIVED PING")
    this.sender().tell("pong")
  }
}

const pinger = system.spawn(new Pinger())

class Ponger extends akkajs.Actor {
  constructor() {
    super()
    this.name = "ponger"
    this.receive = this.receive.bind(this)
  }
  receive(msg) {
    console.log("RECEIVED PONG")
    this.sender().tell("ping")
  }
}

const ponger = system.spawn(new Ponger())

pinger.tell("ping", ponger)

An Actor based greeter on Node looks like follows:

const akkajs = require("akkajs")
const readline = require("readline")

const system = akkajs.ActorSystem.create("helloworld")

class Greeter extends akkajs.Actor {
  constructor() {
     super()
     // following guideline in:  https://facebook.github.io/react/docs/react-without-es6.html#autobinding
     this.receive = this.receive.bind(this)
   }
   receive(msg) {
    console.log("Hello " + msg)
   }
}

const greeter = system.spawn(new Greeter())

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})

rl.question(
  "What is your name? ",
  (answer) => {
    greeter.tell(answer)
    rl.close()
  }
)

Akka.Js is pure Javascript and has not got known limitations in running in the major browsers or directly on any JS VM (e.g. Node, Phantom, ...)

Into the github repo there are few examples under the demo folder.

To run the examples simply clone this project go into the demo directory and run with:

https://github.com/akka-js/akka.js_bindings.git
cd akka.js_bindings/demo
run <example-name>

Full API

Configuration

Configuration class enable you to configure your ActorSystem.

Configuration is based on the HOCON standard ported to JS through ScalaJs thanks to SHOCON

The Configuration constructor accept an optional String as parameter that enable you to override parameters.

all defaults instantiation:

const config = new akkajs.Configuration()

custom instantiation:

const config = new akkajs.Configuration(`akka {
  loglevel = "DEBUG"
  stdout-loglevel = "DEBUG"
}`)

the full default configuration can be found here

Methods

|Method|Description| |-------|----------| |config.add(moreconfig)| To add additional customizations to your configuration (e.g. config.add("akka.log-config-on-start = on")) | |config.get()| To obtain the configuration object to construct an ActorSytem |

ActorSystem

An ActorSystem is a container and the environment for your Actors.

The ActorSystem creator method accept an optional String as name and an optional configuration.

all defaults instantiation:

const system = akkajs.ActorSystem.create()

with custom name:

const system = akkajs.ActorSystem.create("myActorSystem")

with custom name and configuration:

const system = akkajs.ActorSystem.create("myActorSystem", config.get())

Methods

|Method|Description| |-------|----------| |system.terminate()| Shutdown the ActorSystem | |system.select(actorpath)| To obtain the reference to an Actor | |system.spawn(actor)| Will spawn a new instance of the provided Actor and return the reference to it |

Actor

An Actor is is an execution unit that runs concurrently with other actors.

You have to define a class that extends akkajs.Actor and use the spawn method on an instance of it to run your Actor and obtain the reference to it.

class Example extends akkajs.Actor {
  constructor() {
    super()
    this.receive = this.receive.bind(this)
  }
  receive(msg) {
    // do something when you receive a message
  }
}

const actor = system.spawn(new Example())

Actors talk each other only by message passing and you cannot call directly a method on them. Given a reference to an Actor you can invoke the followings:

Methods

|Method|Description| |-------|----------| |actor.path()| Return a String representing a logical path associated to your actor, it can be used to identify an actor using the select method | |actor.tell(msg)| Send to the Actor the specified msg | |actor.tell(msg, anotheractor)| Send to the Actor the specified msg pretending the sender to be anotheractor | |actor.kill()| Will kill the specified actor |

When defining an Actor class you can override a few definitions

Methods

|Method|Description| |-------|----------| |this.name| The name of the Actor will be used to construct the path | |this.receive(msg)| This method gets triggered when dequeuing the mailbox of the actor | |this.preStart()| Triggered once when the Actor is started (or re-started) | |this.postStop()| Triggered once when the Actor is stopped |

an example class that override all of these looks like:

class Example extends akkajs.Actor {
  constructor() {
    super()
    this.name = "newname"
    this.preStart = this.preStart.bind(this)
    this.receive = this.receive.bind(this)
    this.postStop = this.postStop.bind(this)
  }
  preStart() { console.log("starting " + this.name) }
  receive(msg) { console.log("received message: " + msg) }
  postStop() { console.log("stopping") }
}

// and you can trigger all of them by running:
const actor = system.spawn(new Example())
setTimeout(
  () => {
    actor.tell("hello world")
    actor.kill()
  }
)

From within the Actor itself you have access to a few more functions to call:

Methods

|Method|Description| |-------|----------| |this.path()| Return the path of the Actor | |this.parent()| Return the array of the children of this Actor | |this.children()| Return the array of the children of this Actor | |this.sender()| Called within the receive method return the reference to the Actor that sent the message - inferring the sender has some limitations consider using explicit sender in the tell method to have it working reliably | |this.self()| Return the reference to itself | |this.system()| Return the ActorSystem this Actor belong to | |this.spawn(actor)| Let spawn an Actor as a child of this one, return it's reference | |this.become(func)| Change the current receive function to the argument func |

Compile and contribute

You need to install Sbt to compile this project from sources. Compile with

sbt compile

and you can test examples with your modifications by running first:

sbt deploy

and running desired examples into the demo directory.