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

hare

v1.1.0

Published

Wrapper around amqp to make common patterns easier

Downloads

411

Readme

Build Status

Hare

Hare is a wrapper around amqp providing a cleaner chainable API for some of the common patterns.

Installation

npm install hare

Connecting

To connect to your amqp server you can pass in your options to hare

var myHare = hare({url : "amqp://guest:guest@localhost:5672"});

Or you can specify through the connectionOptions method

hare.connectionOptions({url : "amqp://guest:guest@localhost:5672"});

var myHare = hare();

Heartbeat

You may also specify a heartbeat (See here)

hare({url : "amqp://guest:guest@localhost:5672", heartbeat: 2});

//or

hare({url : "amqp://guest:guest@localhost:5672"}).heartbeat(2);

WorkerQueues

Worker queues allow you to ditribute messages to workers, where only one worker will recieve the message. Allowing for the distribution of resource intensive tasks across a worker pool.

To publish to a worker queue.

var queue = myHare.workerQueue("my.queue");

queue.publish({hello : "world"});

To create a worker subscribe to the worker queue.

myHare.workerQueue("my.queue").subscribe(function(message, headers, deliveryInfo, done){
	console.log(message);
	//call done to ack the message	
	done();
});

To read more about worker queues click here

Publish/Subscribe

Publish and Subscribe allows you to broadcast messages to multiple consumers at a time.

To create a pub sub system you can use the pubSub method.

var queue = myHare.pubSub("my-exchange");

setInterval(function () {
   queue.publish({hello: i++});
}, 500);

To subscribe to the topic

myHare.pubSub("my-exchange").subscribe(function (event) {
   console.log("%d, got message %j", process.pid, event);
});

To read more about publishing and subscribing click here

Routing

Routing is similar to pubSub except that subscribers can listen to a subset of messages.

To create a routing system you can use the route method.

var queue = myHare.route("direct_logs");

var LEVELS = ["debug", "info", "warn", "error", "fatal"];

setInterval(function () {
	var level = LEVELS[i++ % LEVELS.length];
	queue.publish(level, {hello:level});
}, 500);

To subscribe to the topics…

hare().route("direct_logs", "debug").subscribe(function (event) {
   console.log("%d, got message %j for level %s", process.pid, event, level);
});

To read more about routing click here

Topics

Topics is similar to routing except that it allows you to subscribe to messages on multiple criteria.

To create a topic queue use the topic method.

var queue = myHre.topic("topic_logs")
var LEVELS = ["log.debug", "log.info", "log.warn", "log.error", "log.fatal"];

setInterval(function () {
   var level = LEVELS[i++ % LEVELS.length];
   queue.publish(level, {hello:level});
}, 500);

To bind to topic yous can use the wildcards:

  • * (star) can substitute for exactly one word.
  • # (hash) can substitute for zero or more words.
myHare.topic("topic_logs", "log.*").subscribe(function(message){

})

Or bind directly.

myHare.topic("topic_logs", "log.info").subscribe(function(message){

})

To read more about topics click here

Rpc

Using the rpc() provides a basic rpc mechanism that can be used for request response style messaging.

To create an rpc queue use the rpc method.


var rpcQueue = hare().rpc("my_queue");

In the server you can provide a handle function which responds to messages.


rpcQueue.handle(function(msg){
    return "hello " + msg.name;
});

If your handler is async you can either return a promise.


rpcQueue.handle(function(msg){
    return new Promise().callback("hello " + msg.name).promise();
});

or invoke the done callback

rpcQueue.handle(function(msg, done){
    return done(null, "hello " + msg.name);
});

In the client you just invoke the call method which sends a message.

The call method returns a promise.

rpcQueue.call({name: "Bob"}).chain(function(res){
    console.log(res); //"hello Bob"
});

Or you can provide a callback

rpcQueue.call({name: "Bob"}, function(err, res){
    console.log(res); //"hello Bob"
});

Creating your own Queue

You may also use the queue method to create your own queue if the above patterns do not match your needs.

For example to create a worker queue manually that is durable, and will not auto delete

var queue = myHare.queue("name").ack(true).durable(true).autoDelete(false);

queue.publish({hello : "world"});

To customize the queue even further you may specify the following options using the chainable api.

  • passive()

  • durable()

  • exclusive()

  • autoDelete()

  • noDeclare()

  • args()

  • closeChannelOnUnsubscribe()

  • exchange()

  • routingKey()

  • ack()

  • prefetchCount()

To read more about the queue options click here

Creating Exchanges

You may also use the exchange method to work with your own exchange.

For example to create a pubsub queue manually you could to the following.

var queue = myHare.exchange("name").type("fanout").queue().exclusive(true);

queue.publish({hello : "world"});

To customize the exchange even further you may specify the following options using the chainable api.

  • passive()
  • type()
  • durable()
  • confirm()
  • comfirm()
  • autoDelete()
  • noDeclare()

To read more about the queue options click here

Logging

Hare comes with a logger which is useful for debugging. By default logging is turned off.

To turn on logging.

hare.log();

To turn off logging.

hare.noLog();

Or to set the level

//only log error messages
hare.logLevel("error");

Configuring Defaults

You can configure defaults for all queues using the queueOptions options.

hare.queueOptions({durable : true, passive : false, autoDelete : false});

License

MIT https://github.com/c2fo/hare/raw/master/LICENSE

Meta