项目作者: dial-once

项目描述 :
BunnyMQ is an amqp.node wrapper to ease common AMQP usages (RPC, pub/sub, channel/connection handling etc.).
高级语言: JavaScript
项目地址: git://github.com/dial-once/node-bunnymq.git
创建时间: 2016-01-19T16:44:36Z
项目社区:https://github.com/dial-once/node-bunnymq

开源协议:MIT License

下载


node-bunnymq

Circle CI
npm
npm
Sonar
Sonar
Sonar
Sonar
Sonar
Sonar

npm

Features

  • Subscriber (consumer)
  • Publisher (producer)
  • RPC (get answers from subscriber automatically)
  • Auto connect/reconnect/queue messages
  • Handle errors / requeing when message callback fails
  • Messages types caring using AMQP headers for content type (send as objects and receive as objects)

Installation

bunnymq requires nodejs 6 or harmony flags! because it uses es6 features outside strict mode.

  1. npm install bunnymq

Basic usage

Publisher

Producer (publisher), can send messages to a named queue.

  1. const bunnymq = require('bunnymq')({ host: 'amqp://localhost' });
  2. bunnymq.publish('queue:name', 'Hello World!');

Subscriber

Consumer (subscriber), can handle messages from a named queue.

  1. const bunnymq = require('bunnymq')({ host: 'amqp://localhost' });
  2. bunnymq.subscribe('queue:name', function (msg) {
  3. //msg is the exact item sent by a producer as payload
  4. //if it is an object, it is already parsed as object
  5. });

RPC Support

You can create RPC requests easily by adding the rpc: true option to the produce call:

  1. bunnymq.subscribe('queue:name', function() {
  2. return 'hello world!'; //you can also return a promise if you want to do async stuff
  3. });
  4. bunnymq.publish('queue:name', { message: 'content' }, { rpc: true, timeout: 1000 })
  5. .then(function(consumerResponse) {
  6. console.log(consumerResponse); // prints hello world!
  7. });

The optional timeout option results in a rejection when no answer has been received after the given amount of milliseconds.
When ‘0’ is given, there will be no timeout for this call.
This value will overwrite the default timeout set in the config in rpcTimeout.

Routing keys

You can send publish commands with routing keys (thanks to @nekrasoft)

  1. bunnymq.publish('queue:name', { message: 'content' }, { routingKey: 'my-routing-key' });

Config

You can specify a config object, properties and default values are:

  1. const bunnymq = require('bunnymq')({
  2. host: 'amqp://localhost',
  3. //number of fetched messages at once on the channel
  4. prefetch: 5,
  5. //requeue put back message into the broker if consumer crashes/trigger exception
  6. requeue: true,
  7. //time between two reconnect (ms)
  8. timeout: 1000,
  9. //default timeout for RPC calls. If set to '0' there will be none.
  10. rpcTimeout: 1000,
  11. consumerSuffix: '',
  12. //generate a hostname so we can track this connection on the broker (rabbitmq management plugin)
  13. hostname: process.env.HOSTNAME || process.env.USER || uuid.v4(),
  14. //the transport to use to debug. if provided, bunnymq will show some logs
  15. transport: utils.emptyLogger
  16. });

You can override any or no of the property above.

Note: if you enable the debug mode using the AMQP_DEBUG=true env var, but you do not attach any transport logger, the module will fallback to console.

Env vars

Deprecated as of 2.1.0, don’t use env vars to configure the module, see Config section.

Documentation & resources

To generate documentation, just run npm run docs, it will create a docs folder.

You can also find more about RabbitMq in the links below:

Tests

Requirements:

  • docker
  • npm
  • make

Run make deps once and then make test to launch the test suite.

License

The MIT License MIT