Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

amqp-simple-pub-sub

1.0.11 • Public • Published

AMQP Simple Pub Sub

A Pub Sub system that uses AMQP messaging to exchange data between services.

NPM

To Use

You project needs to be using at least Node version 8, and ideally Node 10 (LTS) or later.

npm install amqp-simple-pub-sub

Create a Publisher

const { makePublisher } = require('amqp-simple-pub-sub')
const publisher = makePublisher({ exchange: 'testService' })

Publish a message

await publisher.start()
publisher.publish('test', 'Hello World')

Create a Subscriber

const { makeSubscriber } = require('amqp-simple-pub-sub')
 
const subscriber = makeSubscriber({
  exchange: 'testService',
  queueName: 'testQueue',
  routingKeys: ['test']
})

Subscribe to a queue and listen for messages

const handler = message => {
  console.log('Message Received', message)
  subscriber.ack(message)
}
 
subscriber.start(handler)

Other Options

Publisher

The full options object is as follows

{
  type: 'topic', // the default
  url: 'amqp://localhost', // the default
  exchange: 'you must provide this', // it's the name of your service usually
  onError: err => { // optional
    console.error('A connection error happened', err) // or do something clever
  },
  onClose: () => { // optional
    console.log('The connection has closed.') // or do something clever
  }
}

Subscriber

The full options object is as follows

{
  type: 'topic', // the default
  url: 'amqp://localhost', // the default
  exchange: 'you must provide this', // it's the name of your service usually
  queueName: 'you must also provide this', // give your queue a name
  routingKeys: ['an', 'array', 'of', 'routingKeys'], // optional.  Uses [queueName] otherwise.
  onError: err => { // optional
    console.error('A connection error happened', err) // or do something clever
  },
  onClose: () => { // optional
    console.log('The connection has closed.') // or do something clever
  }
}

Examples

See some examples in the tests, and also:

Related Projects

  • amqp-delegate — A library that simplifies, to the point of triviality, use of AMQP based remote workers.
  • ampq-event-tester — A Dockerised and configurable utility to help integration-test your amqp services.

Development

Greenkeeper badge

Branch Tests Code Coverage Comments
develop CircleCI codecov Work in progress
master CircleCI codecov Latest release

Prerequisites

  • NodeJS, version 10.16.3 (LTS) or better (I use nvm to manage Node versions — brew install nvm.)
  • Docker (Use Docker for Mac, not the homebrew version)

Initialisation

npm install

To Start the queue server for integration testing.

docker-compose up -d

Runs Rabbit MQ.

Test it

  • npm test — runs the unit tests (quick and does not need rabbitmq running)
  • npm run test:unit:cov — runs the unit tests with code coverage (does not need rabbitmq)
  • npm run test:integration — runs the integration tests (needs rabbitmq)
  • npm run test:mutants — runs the mutation tests (does not need rabbitmq)

Lint it

npm run lint

Contributing

Please see the contributing notes.

Install

npm i amqp-simple-pub-sub

DownloadsWeekly Downloads

10

Version

1.0.11

License

MIT

Unpacked Size

21.2 kB

Total Files

10

Last publish

Collaborators

  • avatar