From fccb86ea3b099074a67d72fbfb10dbc72822fe82 Mon Sep 17 00:00:00 2001 From: bwhatley Date: Thu, 16 Nov 2017 19:40:43 -0500 Subject: [PATCH] Update readme. --- README.md | 75 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ead3cdb..8a0deb3 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,20 @@ # postmaster-general [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/darklordzw/postmaster-general/blob/master/LICENSE.md) [![Build Status](https://travis-ci.org/darklordzw/postmaster-general.svg?branch=master)](https://travis-ci.org/darklordzw/postmaster-general) [![Coverage Status](https://coveralls.io/repos/github/darklordzw/postmaster-general/badge.svg?branch=master)](https://coveralls.io/github/darklordzw/postmaster-general?branch=master) -Dead-simple, ready-to-use, promise-based Node.js library, for microservice communication over [AMQP][1] using [amqplib][3]. +Dead-simple, ready-to-use, promise-based Node.js library for microservice communication over [AMQP][1] using [amqplib][3]. -This library attempts to provide a simple, ready-to-go microservice message bus with default settings based on production usage. +## Features +postmaster-general strives to be unopinionated, serving as a thin wrapper around [amqplib][3] that sets some sensible defaults and encourages best-practices, including: + +* Dedicated channels for publishing, consuming, and asserting topology. +* Single-purpose listener queues, no mixing messages. +* Built-in RPC support. +* Automatic reconnect on connection failure and channel error. ## Install +Note: This library makes heavy use of the async/await keywords, and requires Node.js 7+. + ```sh npm install --save postmaster-general ``` @@ -15,33 +23,70 @@ npm install --save postmaster-general The following snippet showcases basic usage. ```js -const PostmasterGeneral = require('../postmaster-general'); +const PostmasterGeneral = require('postmaster-general'); -const postmaster = new PostmasterGeneral('pub-sub'); +const postmaster = new PostmasterGeneral({ logLevel: 'debug' }); -// Start the Postmaster instance. -postmaster.addListener('action:get_greeting', function (message, done) { +const printGreeting = async (message) => { console.log('[action:get_greeting] received'); - return done(null, { - greeting: 'Hello, ' + message.name - }); -}) + return { greeting: 'Hello, ' + message.name }; +}; + +// Start the Postmaster instance. +postmaster.connect() + .then(() => postmaster.addListener('action:get_greeting', printGreeting)) // Add a listener callback. - .then(() => postmaster.start()) + .then(() => postmaster.startConsuming()) // Publish a fire-and-forget message. - .then(() => postmaster.publish('action:get_greeting', {name: 'Bob'})) + .then(() => postmaster.publish('action:get_greeting', { name: 'Bob' })) // Publish a message with a callback. - .then(() => postmaster.request('action:get_greeting', {name: 'Steve'})) + .then(() => postmaster.request('action:get_greeting', { name: 'Steve' })) // Handle the callback. .then((res) => { console.log(res.greeting); + }) + // Shut everything down. + .then(() => postmaster.shutdown()) + .catch((err) => { + console.log(err.message); + }) + .then(() => { + process.exit(); }); ``` -### Wildcards -postmaster-general supports the [default AMQP wildcards for topic routes][4]. +### Options +The following options may be overridden in the constructor: +```json +{ + "connectRetryDelay": 1000, + "connectRetryLimit": 3, + "consumerPrefetch": 1, + "deadLetterExchange": "postmaster.dlx", + "exchanges": { + "dlx": { + "name": "postmaster.dlx", + "type": "topic" + }, + "topic": { + "name": "postmaster.topic", + "type": "topic" + } + }, + "handlerTimingResetInterval": 30000, + "heartbeat": 30, + "logLevel": "warn", + "publishRetryDelay": 1000, + "publishRetryLimit": 3, + "queuePrefix": "postmaster.queue", + "removeListenerRetryDelay": 1000, + "removeListenerRetryLimit": 3, + "shutdownTimeout": 30000, + "url": "amqp://guest:guest@localhost:5672" +} +``` ## License Licensed under the [MIT][2] license.