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

resource-mesh

v0.5.1

Published

provides a distributed p2p event emitter mesh

Downloads

223

Readme

mesh

provides a distributed event emitter mesh

Features

  • Distributed WebSocket event emitter
  • Intelligent event broadcasting among mesh
  • Auto-detection of role as server or client
  • Powered by engine.io

Examples

See: ./examples folder

Basic Mesh

This example can be run N times in order to create a mesh of event emitting nodes.

myEvent is added on each node and also emitted on a timer.

Every node will then recieve myEvent from other nodes.

mesh.start({ port: 8888 }, function (err) {

  if (err) {
    throw err;
  }

  mesh.emitter.on('myEvent', function(data){
    console.log('mesh ' + this.event + " - " + data.pid);
  });

  setInterval(function(){a
    mesh.emitter.emit('myEvent', { "pid": process.pid });
  }, 1000);

});

Explicit server / client can be created using mesh.listen and mesh.connect.

For more examples see: ./examples folder

Browser Usage

For browser examples see: ./examples/4_browser/ folder

Frequent Concerns

I can't get my nodes to receive custom events!

Did you bind an event listener using mesh.emitter.on? Events must be bound in order to be seen on the mesh.

How can I listen for events using mesh.emitter.onAny?

By design, mesh.emitter.onAny will not receive remote events. This is in order to keep network traffic to a minimum.

How are events broadcasted among the mesh?

mesh uses a star topography. The first node is the server and all subsequent nodes are clients.

All nodes in the mesh are eligible for receiving events from any other node.

  • Any events emitted on a client will be recieved on the server
    • Provided the server has a listener for that event
  • Any events emitted on the server will re-broadcast to all clients
    • Provided that client has a listener for that event
  • Any events recieved on the server will re-broadcast to all clients
    • Excluding the original client sender
    • Provided that client has a listener for that event

How is network traffic kept low if all nodes are in communication?

An event map of every node is kept and exchanged on connection with the mesh. Events are then only broadcasted to a node if that event name has been previously registered.

Can I bind new live events after a connection is made to the mesh?

Yes! Live events are fully supported. This also means you can interact with the mesh in real-time using a repl.

Is it possible to get a remote callback for emitted mesh events?

The mesh does not support remote callbacks. The overhead of safely enabling remote callbacks requires V8::Persistent<Object> and the use of MakeWeak method through node-weak. Since node core doesn't have access to WeakMaps yet it's somewhat cumbersome to use them.

Apart from these minor technical issues, using remote callbacks is not a great design choice for distributed applications. In the real world actors will crash and message confirmations may get lost ( even though the event was received and fired on the receiving node ). It is generally a better design choice to stick with an event emitter pattern with no built in confirmations. This same functionality of a remote callback can be achieved using two separate named events and a unique message identifier.

Tests

npm test