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

happner-cluster

v13.0.5

Published

Extends happner with clustering capabilities

Downloads

541

Readme

npmBuild StatusCoverage Status

happner-cluster

Extends happner with clustering capabilities.

Install

npm install happner-cluster happn-service-mongo-2 —save

Note data service installed separately.

Starting a cluster node

Happner-cluster and happner configs are almost identical excpet that cluster nodes should include a domain name and the happn subconfigs necessary for clustering - as minimum shown below.

For more on happn-cluster subconfig see happn-cluster docs

var HappnerCluster = require('happner-cluster');

var config = {

  // name: 'UNIQUE_NAME', // allow default uniqie name
  domain: 'DOMAIN_NAME', // same as other cluster nodes, used for event replication - allows clusters to be segmented by domain

  cluster: {
    //  requestTimeout: 20 * 1000, // exchange timeouts
    //  responseTimeout: 30 * 1000
  },

  happn: { // was "datalayer"
    services: {
      data: {
        // see data sub-config in happn-cluster docs
        config: {
          datastores: [
            // defaulted by happn-cluster
            //{
            //  name: 'mongo',
            //  provider: 'happn-service-mongo-2',
            //  isDefault: true,
            //  settings: {
            //    collection: 'happn-cluster',
            //    database: 'happn-cluster',
            //    url: 'mongodb://127.0.0.1:27017'
            //    url: 'mongodb://username:[email protected]:27017,127.0.0.1:27018,127.0.0.1:27019/happn?replicaSet=test-set&ssl=true&authSource=admin'
            //  }
            //},

            // defaulted by happner-cluster to prevent overwrites in shared db
            // where each cluster server requires unique data at certain paths
            //{
            //  name: 'nedb-own-schema',
            //  settings: {},
            //  patterns: [
            //    '/mesh/schema/*',
            //    '/_SYSTEM/_NETWORK/_SETTINGS/NAME',
            //    '/_SYSTEM/_SECURITY/_SETTINGS/KEYPAIR'
            //  ]
            //}
          ]
        }
      }
      membership: {
        // see membership sub-config in happn-cluster docs
      },
      orchestrator: {
          config: {
            minimumPeers: minPeers || 3, //minimum peers before stabilise
            replicate: [
              'my-custom-path/*'
            ] //listen to all cluster events on this path, the following are also listened to by default:
            // `/_events/${config.domain}/*/*`,
            // `/_events/${config.domain}/*/*/*`,
            // `/_events/${config.domain}/*/*/*/*`,
            // `/_events/${config.domain}/*/*/*/*/*`,
            // `/_events/${config.domain}/*/*/*/*/*/*`,
            // `/_events/${config.domain}/*/*/*/*/*/*/*`
            // NB: to not listen to any cluster events apart from security replication, set replicate: false
          }
        }
    }
  },

  modules: {
  },

  components: {
  }
}

HappnerCluster.create(config).then...

Using remote components in the cluster

A component that wishes to use non-local components whose instances reside elsewhere in the cluster should declare the dependencies in their package.json

Given a clusternode with component1...

config = {
  modules: {
    'component1': {
      // using most complex example of module which defines multiple component classes
      path: 'node-module-name',
      construct: {
        name: 'Component1'
      }
    }
  },
  components: {
    'component1': {...}
  }
}

…to enable component1 to use remote components from elsewhere in the cluster...

Component1.prototype.method = function ($happner, callback) {
  // $happner aka $happn
  // call remote component not defined locally
  $happner.exchange['remote-component'].method1(function (e, result) {
    callback(e, result);
  });

  // also
  // $happner.event['remote-component'].on() .off() .offPath()
}

…it should declare the dependency in its package.json file…

// package.json expressed as js
{
  name: 'node-module-name',
  version: '1.0.0',
  happner: {
    dependencies: {
      'component1': { // the component name which has the dependencies
                      // (allows 1 node_module to define more than 1 mesh component class)
        'remote-component': {
          version: '^1.0.0', // will only use matching versions from
                             // elsewhefre in the cluster
          methods: { // list of methods desired on the remote compnoent - these will not be discovered and will need to be statically defined
            method1: {},
            method2: {}
          }
        },
        'remote-component2': {
          version: '~1.0.0'
          // no methods, only interested in events
        },
        'remote-component3': {
          version: '~1.0.0'
          discoverMethods: true //this special switch will result method discovery for the component
        }
      }
    }
  }
}

// NB: if method discovery is switched on, inside your component method - where $happn is passed in be aware that the methods may only be available after startup - as the mesh description of the arriving peer on the cluster is used to generate the method api for the discovered component, this means that the declarative approach using $call with $happn should be used to ensure the api does not break, ie:

//dont do this
await $happn.exchange['remote-component3'].discoveredMethod(arg1, arg2);


//rather do this, if the method is not around you can at least handle the method missing error
try {
  await $happn.exchange.$call({
    component: 'remote-component3',
    method: 'discoveredMethod',
    arguments: [arg1, arg2]
  });
} catch (e) {
  if (e.message === 'invalid endpoint options: [remote-component3.discoveredMethod] method does not exist on the api') {
    methodMissingHandler();
  }
  throw e;
}

Note:

  • If a component is defined locally and remotely then local is preferred and remote never used.
  • If the component is defined on multiple remote nodes, a round-robin is performed on the method calls.

Brokered components using $broker

Using special syntax in the package.json happner config, it is possible to broker remote dependencies as if they were local components served up by the mesh

The following is an example package.json of a component that is brokering requests to the internal dependency remoteComponent, note the special $broker dependency name, which instructs the cluster to inject remoteComponent into the meshes exchange:

{
  "name": "broker-component",
  "version": "1.0.1",
  "happner": {
    "dependencies": {
      "$broker": {
        "remoteComponent": {
          "version": "^2.0.0",
          "methods": {
            "brokeredMethod1": {},
            "brokeredEventEmitMethod":{}
          }
        }
      }
    }
  }
}

Based on the above setup, clients are now able to connect to an edge cluster node (which has declared the broker component) and call the brokered dependencies as if they were loaded as components on the edge node:


var client = new Happner.MeshClient({
  hostname: 'localhost',
  port: 8080
});

client.login({
    username: 'username',
    password: 'password'
  })
  .then(function () {
    //NB NB: remoteComponent is now injected into the exchange as if the internal component
    // were a declared component on the edge cluster node:
    return client.exchange.remoteComponent.brokeredMethod1();
  })
  .then(function(result){
    //happy days...
  })

Note:

  • Duplicate injected dependencies (components with the same name brokered from different brokers) will fail to load, even if they are pointing to internal components with different versions.