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

nest-cluster-hub

v0.4.0

Published

A Nest module for communication between master and worker processes

Downloads

220

Readme

nest-cluster-hub

NPM version

A Nest module for communication between master and worker processes based on node-cluster-hub

Installation

To begin using it, we first install the required dependency.

$ npm install --save nest-cluster-hub

Getting started

Once the installation is complete, import the ClusterHubModule into the root AppModule and run the forRoot() static method as shown below:

import { Module } from '@nestjs/common';
import { ClusterHubModule } from 'nest-cluster-hub';

@Module({
  imports: [
    ClusterHubModule.forRoot(),
  ],
})
export class AppModule {}

Next, inject the ClusterHub instance using the @InjectClusterHub() decorator.

constructor(@InjectClusterHub() private readonly hub: ClusterHub) {}

Sending messages

Sending a message to a specific worker

if (cluster.isPrimary) {
  this.hub.sendToWorker(worker, 'master-to-worker', 1);
}

You can just pass any string instead of cluster.Worker. The library uses HashRing to let you find the correct worker for the key which is closest to the point after what the given key hashes to.

Sending a message to a random worker

if (cluster.isPrimary) {
  this.hub.sendToRandomWorker('master-to-worker', 1);
}

Sending (broadcasting) a message to all workers

if (cluster.isPrimary) {
  this.hub.sendToWorkers('master-to-worker', 1);
}

Sending a message to the master (primary)

if (cluster.isPrimary) {
  this.hub.sendToMaster('master-to-master', 1);
}

if (cluster.isWorker) {
  this.hub.sendToMaster('worker-to-worker', 1);
}

Handling received messages

if (cluster.isPrimary) {
  this.hub.on('master-to-master', (data) => {
    console.log('master-to-master received');
  });

  this.hub.on('worker-to-master', (data) => {
    console.log('worker-to-master received');
  });
}

if (cluster.isWorker) {
  this.hub.on('master-to-worker', (data) => {
    console.log('master-to-worker received');
  });
}

Handling received messages with @OnMessage() decorator

@OnMessage('master-to-worker')
async handleMessage(data) {
  console.log('master-to-worker received');
}

Request-response

Sending a request to a specific worker

if (cluster.isPrimary) {
  this.hub.requestWorker(worker, 'mult', { a: 5, b: 7 }, (err, sum) => {
    console.log('Mult in master: ' + sum);
  });
}

You can also pass any string instead of cluster.Worker to find the correct worker.

Sending a request to a random worker

if (cluster.isPrimary) {
  this.hub.requestRandomWorker('mult', { a: 5, b: 7 }, (err, sum) => {
    console.log('Mult in master: ' + sum);
  });
}

Sending a request to the master (primary)

if (cluster.isPrimary) {
  this.hub.requestMaster('sum', { a: 5, b: 7 }, (err, sum) => {
    console.log('Sum in master: ' + sum);
  });
}

if (cluster.isWorker) {
  this.hub.requestMaster('sum', { a: 5, b: 7 }, (err, sum) => {
    console.log('Sum in worker: ' + sum);
  });
}

Handling requests and responses

if (cluster.isPrimary) {
  this.hub.on('sum', (data, sender, callback) => {
    callback(null, data.a + data.b);
  });
}

if (cluster.isWorker) {
  this.hub.on('mult', (data, sender, callback) => {
    callback(null, data.a * data.b);
  });
}

Handling requests and responses with @OnRequest() decorator

@OnRequest('mult')
async handleMessage(data, sender, callback) {
  callback(null, data.a * data.b);
}

Sharing data between processes

this.hub.set('foo', 'bar', () => {
  this.hub.get('foo', (err, value) => {
    console.log(value === 'bar'); // true
  });
});

Retrieving existing workers

Finding a worker

if (cluster.isPrimary) {
  this.hub.getWorker('foo');
}

Retrieving all workers

if (cluster.isPrimary) {
  this.hub.getWorkers();
}

Example

A working example is available here.

License

MIT