A simple Pub Sub system that uses AMQP Messaging to exchange data between services
You project needs to be using at least Node version 8, and ideally Node 10 (LTS) or later.
npm install amqp-simple-pub-sub
const { makePublisher } = require('amqp-simple-pub-sub')
const publisher = makePublisher({ exchange: 'testService' })
await publisher.start()
publisher.publish('test', 'Hello World')
const { makeSubscriber } = require('amqp-simple-pub-sub')
const subscriber = makeSubscriber({
exchange: 'testService',
queueName: 'testQueue',
routingKeys: ['test']
})
const handler = message => {
console.log('Message Received', message)
subscriber.ack(message)
}
subscriber.start(handler)
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
}
}
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
}
}
See some examples in the tests, and also:
- competing-services-example
- And the associated article: itnext.io/connecting-competing-microservices-using-rabbitmq
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.
- NodeJS, version 10.15.1 (LTS) or better (I use
nvm
to manage Node versions —brew install nvm
.) - Docker (Use Docker for Mac, not the homebrew version)
npm install
docker-compose up -d
Runs Rabbit MQ.
npm test
— runs the unit tests (quick and does not need rabbit mq running)npm run test:integration
— runs the integration tests (not so quick and needs rabbitmq running)
npm run lint
Please see the contributing notes.