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

cluster-synapse

v0.2.3

Published

Event like cluster communication tool.

Downloads

4

Readme

cluster-synapse

version npm downloads liscense

cluster-synapse is an easy to use, minimalistic cluster communication tool. It provides a non-intrusive wrapper around the native cluster module and gives you an event like API to send messages between the worker and master process, or between worker processes.

It does not require you to make any changes to your current cluster setup.

=> detailed API docs

Installation

npm install --save cluster-synapse

cluster-synapse does not have any external dependencies.

Usage

setup

You have to require cluster-synapse at least once on the master process, to hook into the cluster module. Even if you do not use cluster-synapse directly on master.

master.js

const cluster 	= require('cluster')
const os 	= require('os')
const synapse 	= require('cluster-synapse') // <= here

const numWorkers = os.cpus().length

cluster.setupMaster({ exec: 'worker.js' })

for(let i = 0; i < numWorkers; i++) {

	cluster.fork()

}

Everywhere else you can just require and use cluster-synapse.

const synapse 	= require('cluster-synapse')

sending messages

master.js

The master process sends messages to all workers.

// send message to all worker processes without data
synapse.emit('justCalling')

// send message to all worker processes with data
synapse.emit('somethingForYou', { gift: 'Surprise!' })

worker.js

By default a worker sends messages to other workers via the master process. The master process is able to intercept worker messages by adding a listener to the messages type (synapse.on('someType', interceptingFunction)).

// send message without data
synapse.emit('helloWorld')

// send message with data
synapse.emit('hereComesData', { stuff: 'I made this up...'})

// send message to all workers (including the worker sending the message)
synapse.emit('thisWillComeBackToMe', { stuff: 'Just stuff'}, true)

receiving messages

master.js

Setting a listener on the master process intercepts the message a worker is sending. Thus, making the master process responsible for further handling the message. The listener receives a message object containing an instance of the sending worker, data (if send) and the type.

// receive message from worker, without notifying other worker processes (single worker to master communication)
synapse.on('msgToMaster', msg => {

	const worker = msg.worker
	const data = msg.data
	const type = msg.type // in this case 'msgToMaster'

	/* do something with data */
})

// receiving a message from a worker process and sending a message to all workers
synapse.on('msgToMaster', msg => {

	/* do something with data */

	synapse.emit('msgFromMaster', { msg: 'Master did something and wants to tell you...'})

})

worker.js

If not intercepted by the master process, the worker process receives messages and the associated data by registering a listener on a specific event.


synapse.on('randomEvent', () => {

	/* event without data */
})

synapse.on('differentEventWithData', data => {

	/* Do something with data */
})

manipulating message data

If you just want to manipulate data and send them back to the worker processes, you can use synapse.transform in the master process.

// 
synapse.transform('dataToProcess', data => {
	
	data.foo = 'bar'

	return data

})