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

tiny-zmq

v2.0.1

Published

A NodeJS package that provides simple load balanced and broadcast messaging on distributed environments

Downloads

7

Readme

TinyZMQ

Tiny ZMQ is an NodeJS package that provides simple, load balanced and resilient messaging on distributed environments.

It implements the Client - Broker - Worker schema and takes care of reachability. The stock ZMQ implementation for NodeJS may run into issues if some of the workers get offline.

An example Docker container is also provided, for convenience.

Installation

Install it from NPM

npm i -S tiny-zmq

Note: You need to have libzmq installed in your computer in order to install the dependencies (zmq).

TinyZMQ implements two communication patterns: Work balancing (ventilator) and Data Broadcast.

Work balancing

Follow this pattern if you have many nodes requesting operations, several workers and need a central broker to do load balancing.

Broker

Create a component that acts as the broker:

const broker = require('tiny-zmq').balancing.broker;

broker.bind({
	clientsPort: 5559,
	workersPort: 5560
});

Client

To register a client, add this code to the component that places requests:

const client = require('tiny-zmq').balancing.client;

const sendRequest = client.connect('tcp://localhost:5559');

sendRequest({ number: 1234 }, function(response){
	console.log("THE CLIENT GOT BACK:", response);
});

Worker

To register a worker, add this code to the component accepting work requests:

const worker = require('tiny-zmq').balancing.worker;

worker.connect('tcp://localhost:5560', function(parameters, doneCallback){
	console.log("THE WORKER GOT", parameters);

	const result = doProcessing(parameters);
	doneCallback(result);
});

function doProcessing(parameters){
	parameters.value = Math.random(); // just appends a random number
	return parameters;
}

Data broadcast

Follow this pattern if you have many nodes generating events, many nodes that need to be notified of them and a central broker to broadcast the data.

Broker

Create a component that acts as the broker:

const broker = require('tiny-zmq').broadcast.broker;

broker.bind({
	clientsPort: 5559,
	subscribersPort: 5560
});

Client

To register a client, add this code to the component that places requests:

const client = require('tiny-zmq').broadcast.client;

const sendRequest = client.connect("tcp://localhost:5559");

var i = 0;
setInterval(function(){
	var request = { number: ++i, pid: process.pid };

	console.log("THE CLIENT NOTIFIES", request);

	sendRequest(request);
}, 2000);

Subscriber

To register a subscriber, add this code to the component:

const worker = require('tiny-zmq').broadcast.subscriber;

worker.connect("tcp://localhost:5560", function(payload){
	console.log("THE SUBSCRIBER GOT", payload);

	// HANDLE THE NOTIFICATION HERE
});

Parameterization

The internal behavior can be tuned by using environment variables at run time.

  • TINY_ZMQ_DEBUG: By default, disabled when NODE_ENV='production' and true otherwise. When set to true, provides extra logging information.
  • TINY_ZMQ_PING_BASE_INTERVAL: The client will periodically ping the worker to assert that it is still alive. By default, every 500 milliseconds.
  • TINY_ZMQ_INACTIVITY_TIMEOUT: After no pingback response from either the client or the worker, the connection will be retried again. By default, the timeout is 5000 milliseconds.
  • TINY_ZMQ_CLIENT_INSTANCES: The number of client instances running on the environment. This helps to better adjust the actual rate of pings between nodes.
  • TINY_ZMQ_WORKER_INSTANCES: The number of worker instances running on the environment.

Utilities

To get an example of a container running the broker, worker or client on a Linux + NodeJS + ZMQ environment, refer to the Dockerfile.

To build a test image for each component, run the appropriate command from the project folder:

docker build --build-arg target=broker -t img_broker .
docker build --build-arg target=worker -t img_worker .
docker build --build-arg target=client -t img_client .

Once the images are built, you can run them inside a container:

docker run -it --name my_broker img_broker
docker run -it --name my_worker img_worker
docker run -it --name my_client img_client

Note: By default, the broker IP is assumed to be 172.17.0.2. To change it, add a build argument:

docker build --build-arg target=worker --build-arg broker_ip=172.17.0.5 -t img_worker .

docker build --build-arg target=client --build-arg broker_ip=172.17.0.5 -t img_client .

Related


by Jordi Moraleda - Tvrbo