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

nanomsg-cluster

v0.7.3

Published

Nanomsg based clustering

Downloads

48

Readme

Nanomsg Cluster

Nanomsg based clustering.

CircleCI npm version

Usage

yarn install nanomsg-cluster

Peers bootstrap off of each other.

Server A, 192.168.1.1

const ClusterNode = require('nanomsg-cluster');

const node = new ClusterNode();

Server B, 192.168.1.2

const ClusterNode = require('nanomsg-cluster');

const node = new ClusterNode({
  peerAddresses: [
    {
      host: '192.168.1.1'
    }
  ]
});

Server C, 192.168.1.3

const ClusterNode = require('nanomsg-cluster');

const node = new ClusterNode();

node.addPeer({host: '192.168.1.1'});

Options

const ClusterNode = require('nanomsg-cluster');

const node = new ClusterNode({
  bindAddress: {
    host: '127.0.0.1', // Optional, default '127.0.0.1'
    pubsubPort: 13001, // Optional, default 13001
    pipelinePort: 13002, // Optional, default 13002
  },
  peerAddresses: [
    {
      host: '127.0.0.1', // Required
      pubsubPort: 13021, // Optional, default 13001
      pipelinePort: 13022, // Optional, default 13002
    }
  ]
});

Methods

Subscribe to a topic:

const topic = "example";
const callback = (message, sender) => {
  console.log("Message", message);
  console.log("Sender", sender);
}

node.subscribe(topic, callback);
node.subscribe(topic, callback, true); // include local broadcasts

// later

node.subscribe(topic, callback);
// or for all callbacks
node.subscribe(topic);

Broadcast to all nodes:

const topic = "example";
const message = {foo: "bar"};

node.sendToAll(topic, message);

Send to a specific node:

const topic = "example";
const message = {foo: "bar"};
const name = "node-2";

node.sendToPeer(name, topic, message);

Send to a pipeline:

const topic = "pipeline example";

node.providePipeline(topic);

// later

const message = {foo: "bar"};
node.sendToPipeline(topic, message);

// Check if node is leader of this pipeline 
// (must be a provider and a consumer)

node.isPipelineLeader(topic) // true or false

Subscribe to a pipeline:

const topic = "pipeline example";
const pipelineSubscriptionPort = 14000;

node.consumePipeline(pipelineSubscriptionPort, topic);

// later

const callback = (message, sender) => {
  console.log("Message", message);
  console.log("Sender", sender);
}
node.subscribe(topic, callback);

// later

node.stopConsumingPipeline(topic);
node.unsubscribe(topic, callback);

Add a peer:

node.addPeer({
  host: '192.168.1.2', // Required
  pubsubPort: 13001, // Optional, default 13001
  pipelinePort: 13002, // Optional, default 13002
});

Remove a peer:

// Returns a Promise.
node.removePeer({
  host: '192.168.1.2', // Required
  pubsubPort: 13001, // Optional, default 13001
  pipelinePort: 13002, // Optional, default 13002
});

Automatic peer discovery with node-discover

const options = {}; // See options at https://github.com/wankdanker/node-discover#constructor

// Returns a Promise.
node.startDiscovery(options);

// later
node.stopDiscovery();

Get a list of peers:

node.getPeers();

// Returns:
//
// [
//   {
//     name: "node-2",
//     host: '192.168.1.2',
//     pubsubPort: 13001,
//     pipelinePort: 13002,
//   },
//   {
//     name: "node-3",
//     host: '192.168.1.3',
//     pubsubPort: 13001,
//     pipelinePort: 13002,
//   },
// ]

Leave the cluster and close sockets:

// Returns a Promise.
node.close();