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

spiders.js

v2.8.9

Published

Parallel and Distributed Programming in JavaScript made easy through actors

Downloads

13

Readme

Spiders.js

Parallel and Distributed Programming in JavaScript made easy through actors

Usage

Install with npm:

npm install spiders.js

Web workers and child processes are basically the only parallel building blocks available to parallelise applications in JavaScript. Let’s face it, parallel programming is hard and dealing with the web workers or Node.js' child processes API doesn’t make it easier. Spiders.js offers a comprehensible API based on communicating event loops which allow you to easily spawn actors in parallel. Moreover, these actors can natively communicate with any other Spiders.js actor (whether these actors reside on the same machine or not).

Tutorial

Actors allow you to deal with parallelism without the hassles of data races and shared mutable state. An actor is basically a thread running in complete isolation which communicates with other actors via message passing. Communicating event loops are a special flavour of actors which add object-oriented constructs to this parallel model.

Spiders.js basics

Any Spiders.js app must create an instance of the application class. This instance will serve as an actor factory and as a broker between your parallel code and the "outside" world (e.g. the DOM).

var spiders = require('spiders.js')
class HelloWorldApp extends spiders.Application{
  hello(message){
    console.log("Hello " + message)
  }
}
var app = new HelloWorldApp()

Actors are defined by extending the actor class and are spawned by providing their class to the spawnActormethod of the Application instance. The method returns a reference to the actor which allows you to asynchronously invoke its methods. Each actor has a parent reference which points to its spawner.

class HelloWorldActor extends spiders.Actor{
  world(){
    this.parent.hello("world")
  }
}
var actorRef = app.spawnActor(HelloWorldActor)
actorRef.world()

Run the example with:

node examples/HelloWorld.js

Same API, client or server

Spiders.js provides the same API whether the application is running on a server or in a browser. client-side actors are implemented using web workers, while server-side actors run atop child processes. Moreover, Spiders.js is browserifiable !

Built-in distribution

All spiders.js actors communicate by asynchronously invoking methods on each other. This communication scheme extends to actors residing on different machines. As an example of how you can implement distributed application using Spiders.js consider the following distributed ping/pong example. The application's server simply allows the ping and pong clients to register themselves. It provides two methods registerPing and registerPong to this end. Each of these methods is invoked by a client which provides a reference to itself.

var spider = require('spiders.js')
class Server extends spider.Application{

    constructor(){
        super()
        this.pingerRef
        this.hasPing = false
        this.pongerRef
        this.hasPong = false
    }

    registerPing(pingerRef){
        this.pingerRef  = pingerRef
        this.hasPing    = true
        if(this.hasPong){
            this.pingerRef.meet(this.pongerRef)
            this.pongerRef.meet(this.pingerRef)
        }
    }

    registerPong(pongerRef){
        this.pongerRef  = pongerRef
        this.hasPong    = true
        if(this.hasPing){
            this.pongerRef.meet(this.pingerRef)
            this.pingerRef.meet(this.pongerRef)
        }
    }
}

new Server("127.0.0.1",8000)

The client first needs to invoke the appropriate register method on the server. It acquires a reference to the server through the remote method, which takes the server's address and port number as arguments and returns a promise which resolves with a reference to the server. Subsequently the client registers itself and awaits for the server to introduce it to its peer (i.e. via the meet method).

var spider = require('spiders.js')
class Pinger extends spider.Application{
    constructor(){
        super()
        this.libs.remote("127.0.0.1",8000).then((serverRef)=>{
            serverRef.registerPing(this)
        })
    }
    meet(pongerRef){
        pongerRef.ping()
    }

    pong(){
        document.getElementById("text").value = "Received Pong!"
    }
}
new Pinger()

Run the example by starting the server:

node examples/Distributed/server.js

and opening both ping.html and pong.html pages.