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

node-swarm-kb

v0.5.1

Published

Kubernetes based inter-cluster communication

Downloads

45

Readme

node-swarm-kb

js-semistandard-style

This library enable communication between each nodes of a Kubernetes based cluster such as openshift.

(Designed for node.js 8+ and Linux)

Purpose

It allows to:

  • Keep track real-time of the cluster topology
  • Notify other nodes of the cluster including Broadcasting messages
  • Cluster Task processing management based on a consistent hashing ring on the cluster servers

Use-case examples:

  • A processing task needs to be executed only once in the cluster (eg. cronjob like).
  • A task needs to be executed on the same server for a given customer (stickiness).
  • Requests queuing & re-ordering to ensure request received by different servers of the cluster get processed sequencially.
  • An event needs to be propagated to every nodes of the cluster (eg. Websocket message or closure).
  • Maintain a client connection map (eg. websocket)

This project will also trigger the 'SIGTERM' event once the local node has been removed from the cluster. So the application has a heads up before its instance get terminated.

Installation

npm i --save node-swarm-kb

Usage

Important: The cluster needs to open the port 45892 for internal communication (You can specify your own port in the options). This port should not be accessible from the outside world!

const Swarm = require('node-swarm-kb');

Swarm.init({
  appName: 'myapp',
  // event listener here
});

Full example: /test/index.js

Cluster Topology

nodes - The list of nodes in the cluster

  swarm.nodes[ip] // object indexed by the node ip addresses

nodeAdded - A new node has been added to the cluster

  swarm.on('nodeAdded', (node) => {
    console.log(`Node ${node.ip} has been added.`);
  });

nodeRemoved - A node has been removed from the cluster

  swarm.on('nodeRemoved', (node) => {
    console.log(`Node ${node.ip} has been removed.`);
  });

amIMaster - Is the current node the Cluster master

The master node do not have any specific role or behavior. It is just an arbitrary selected single node of cluster known by all. This is useful for a behavior that has to be done only in one pod of cluster. The Master is automatically elected after a topology change.

  swarm.amIMaster();

isMe - Test if the given node or ip is the current node.

  swarm.isMe(node || ip);

Messaging

Sending a message to a node

  const promise = swarm.send(ip || node, 'topic', {/* data to send */});

Broadcasting

  const promise = swarm.broadcast('topic', 'Hello everyone');

Listening for messages

  const promise = swarm.messages.on('topic', (message, topics, from) => {
    /* handle message */
    return 'response'; // A Promise can be returned to
  });

Cluster Tasks

This module also includes a way to execute tasks centrally in a cluster. The task will use consistent hashing to execute a given task in a consistent node of the cluster.

Example


Swarm.tasks.register('taskName',
(arg1, arg2) => { /** should return a Bluebird compatible promise **/ },
{ /** Options overloading the default */
  singleTrigger: true
});

// The first argument is used as hashing key.
// If no argument is provided, the task name is used.
const promise = Swarm.tasks.exec('taskName', arg1, arg2);

All Options

  • kubernetes Options for the kubernetes module, used to maintain the list of nodes of the cluster
  • kubernetes.appName the service name, default got from env: OPENSHIFT_BUILD_NAME or HOSTNAME without -XXX postfix
  • kubernetes.folderPath the folder to find kubernetes credentials, default env: KUBERNETES_FOLDER_PATH or /var/run/secrets/kubernetes.io/serviceaccount
  • kubernetes.host kubernetes service host, default env: KUBERNETES_SERVICE_HOST or kubernetes.default.svc.cluster.local,
  • kubernetes.port kubernetes service port, default env: KUBERNETES_SERVICE_PORT or 443,
  • kubernetes.selector selector to find this service in Kubernetes, default env: KUBERNETES_SELECTOR or build from app={appName}
  • kubernetes.token security token, default get from folderPath/token
  • kubernetes.namespace namespace, default get from folderPath/namespace
  • kubernetes.ca ssl sertificate, default get from folderPath/ca.crt
  • kubernetes.refreshInterval how often topology changes are checked, default 10000 ms
  • messages Options for the messaging module
  • messages.port If no http server provide: communication ports, default env: SWARM_MESSAGING_PORT or 45892
  • tasks Options for the task processing module use as default for each tasks
  • tasks.timeout time in ms after which to timeout the task execution, default 30000 ms
  • tasks.serialized Indicate the tasks needs to be executed sequentially, default true
  • tasks.singleTrigger (if serialized == true) Indicate the tasks dont need to accumulate and only one will be executed. default false. Values: 'N' Indicate that the trigger needs to be executed anytime AFTER the trigger and cannot be part of on-going task, therefore queuing a single execution.
  • tasks.maxQueueLength (if serialized = true && singleTrigger = false) The maximum size of the execution queue before an error is returned, default 20