0.7.3 • Published 2 years ago

nanomsg-cluster v0.7.3

Weekly downloads
39
License
MIT
Repository
github
Last release
2 years ago

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();
0.7.2

2 years ago

0.7.1

2 years ago

0.7.3

2 years ago

0.7.0

2 years ago

0.6.19

3 years ago

0.6.18

4 years ago

0.6.17

4 years ago

0.6.16

4 years ago

0.6.15

4 years ago

0.6.14

4 years ago

0.6.13

4 years ago

0.6.12

4 years ago

0.6.11

4 years ago

0.6.10

4 years ago

0.6.9

4 years ago

0.6.8

4 years ago

0.6.7

4 years ago

0.6.5

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago