|
4 | 4 |
|
5 | 5 | A simple Pub Sub system that uses AMQP Messaging to exchange data between services
|
6 | 6 |
|
7 |
| -* `develop` — [](https://circleci.com/gh/davesag/amqp-simple-pub-sub/tree/develop) [](https://codecov.io/gh/davesag/amqp-simple-pub-sub) |
8 |
| -* `master` — [](https://circleci.com/gh/davesag/amqp-simple-pub-sub/tree/master) [](https://codecov.io/gh/davesag/amqp-simple-pub-sub) |
| 7 | +<!-- prettier-ignore --> |
| 8 | +| Branch | Tests | Code Coverage | Comments | |
| 9 | +| ------ | ----- | ------------- | -------- | |
| 10 | +| `develop` | [](https://circleci.com/gh/davesag/amqp-simple-pub-sub/tree/develop) | [](https://codecov.io/gh/davesag/amqp-simple-pub-sub) | Work in progress | |
| 11 | +| `master` | [](https://circleci.com/gh/davesag/amqp-simple-pub-sub/tree/master) | [](https://codecov.io/gh/davesag/amqp-simple-pub-sub) | Latest release | |
9 | 12 |
|
10 | 13 | [](https://nodei.co/npm/amqp-simple-pub-sub/)
|
11 | 14 |
|
12 | 15 | ## To Use
|
13 | 16 |
|
14 | 17 | You project needs to be using at least Node version 8, and ideally Node 10 (LTS) or later.
|
15 | 18 |
|
16 |
| - npm install amqp-simple-pub-sub |
| 19 | +``` |
| 20 | +npm install amqp-simple-pub-sub |
| 21 | +``` |
17 | 22 |
|
18 | 23 | ### Create a Publisher
|
19 |
| - const { makePublisher } = require('amqp-simple-pub-sub') |
20 |
| - const publisher = makePublisher({ exchange: 'testService' }) |
| 24 | + |
| 25 | +``` |
| 26 | +const { makePublisher } = require('amqp-simple-pub-sub') |
| 27 | +const publisher = makePublisher({ exchange: 'testService' }) |
| 28 | +``` |
21 | 29 |
|
22 | 30 | ### Publish a message
|
23 | 31 |
|
24 |
| - await publisher.start() |
25 |
| - publisher.publish('test', 'Hello World') |
| 32 | +``` |
| 33 | +await publisher.start() |
| 34 | +publisher.publish('test', 'Hello World') |
| 35 | +``` |
26 | 36 |
|
27 | 37 | ### Create a Subscriber
|
28 | 38 |
|
29 |
| - const { makeSubscriber } = require('amqp-simple-pub-sub') |
| 39 | +``` |
| 40 | +const { makeSubscriber } = require('amqp-simple-pub-sub') |
30 | 41 |
|
31 |
| - const subscriber = makeSubscriber({ |
32 |
| - exchange: 'testService', |
33 |
| - queueName: 'testQueue', |
34 |
| - routingKeys: ['test'] |
35 |
| - }) |
| 42 | +const subscriber = makeSubscriber({ |
| 43 | + exchange: 'testService', |
| 44 | + queueName: 'testQueue', |
| 45 | + routingKeys: ['test'] |
| 46 | +}) |
| 47 | +``` |
36 | 48 |
|
37 | 49 | ### Subscribe to a queue and listen for messages
|
38 | 50 |
|
39 |
| - const handler = message => { |
40 |
| - console.log('Message Received', message) |
41 |
| - subscriber.ack(message) |
42 |
| - } |
| 51 | +``` |
| 52 | +const handler = message => { |
| 53 | + console.log('Message Received', message) |
| 54 | + subscriber.ack(message) |
| 55 | +} |
43 | 56 |
|
44 |
| - subscriber.start(handler) |
| 57 | +subscriber.start(handler) |
| 58 | +``` |
45 | 59 |
|
46 | 60 | ### Other Options
|
47 | 61 |
|
48 | 62 | #### Publisher
|
49 | 63 |
|
50 | 64 | The full options object is as follows
|
51 | 65 |
|
52 |
| - { |
53 |
| - type: 'topic' // the default |
54 |
| - url: 'amqp://localhost' // the default |
55 |
| - exchange: 'you must provide this' // it's the name of your service usually |
56 |
| - onError: err => { // optional |
57 |
| - console.error('A connection error happened', err) // or do something clever |
58 |
| - } |
59 |
| - onClose: () => { // optional |
60 |
| - console.log('The connection has closed.') // or do something clever |
61 |
| - } |
62 |
| - } |
| 66 | +``` |
| 67 | +{ |
| 68 | + type: 'topic', // the default |
| 69 | + url: 'amqp://localhost', // the default |
| 70 | + exchange: 'you must provide this', // it's the name of your service usually |
| 71 | + onError: err => { // optional |
| 72 | + console.error('A connection error happened', err) // or do something clever |
| 73 | + }, |
| 74 | + onClose: () => { // optional |
| 75 | + console.log('The connection has closed.') // or do something clever |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
63 | 79 |
|
64 | 80 | #### Subscriber
|
65 | 81 |
|
66 | 82 | The full options object is as follows
|
67 | 83 |
|
68 |
| - { |
69 |
| - type: 'topic' // the default |
70 |
| - url: 'amqp://localhost' // the default |
71 |
| - exchange: 'you must provide this' // it's the name of your service usually |
72 |
| - queueName: 'you must also provide this' // give your queue a name |
73 |
| - routingKeys: ['an', 'array', 'of', 'routingKeys'] // optional. Uses [queueName] otherwise. |
74 |
| - onError: err => { // optional |
75 |
| - console.error('A connection error happened', err) // or do something clever |
76 |
| - } |
77 |
| - onClose: () => { // optional |
78 |
| - console.log('The connection has closed.') // or do something clever |
79 |
| - } |
80 |
| - } |
| 84 | +``` |
| 85 | +{ |
| 86 | + type: 'topic', // the default |
| 87 | + url: 'amqp://localhost', // the default |
| 88 | + exchange: 'you must provide this', // it's the name of your service usually |
| 89 | + queueName: 'you must also provide this', // give your queue a name |
| 90 | + routingKeys: ['an', 'array', 'of', 'routingKeys'], // optional. Uses [queueName] otherwise. |
| 91 | + onError: err => { // optional |
| 92 | + console.error('A connection error happened', err) // or do something clever |
| 93 | + }, |
| 94 | + onClose: () => { // optional |
| 95 | + console.log('The connection has closed.') // or do something clever |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
81 | 99 |
|
82 | 100 | #### Examples
|
83 | 101 |
|
84 | 102 | See some examples in the tests, and also:
|
85 | 103 |
|
86 |
| -* [competing-services-example](https://github.com/davesag/competing-services-example) |
87 |
| -* And the associated article: [itnext.io/connecting-competing-microservices-using-rabbitmq](https://itnext.io/connecting-competing-microservices-using-rabbitmq-28e5269861b6) |
| 104 | +- [competing-services-example](https://github.com/davesag/competing-services-example) |
| 105 | +- And the associated article: [itnext.io/connecting-competing-microservices-using-rabbitmq](https://itnext.io/connecting-competing-microservices-using-rabbitmq-28e5269861b6) |
88 | 106 |
|
89 | 107 | ## Related Projects
|
90 | 108 |
|
91 |
| -* [`amqp-delegate`](https://github.com/davesag/amqp-delegate) — A library that simplifies, to the point of triviality, use of AMQP based remote workers. |
92 |
| -* [`ampq-event-tester`](https://github.com/davesag/amqp-event-tester) — A Dockerised and configurable utility to help integration-test your amqp services. |
| 109 | +- [`amqp-delegate`](https://github.com/davesag/amqp-delegate) — A library that simplifies, to the point of triviality, use of AMQP based remote workers. |
| 110 | +- [`ampq-event-tester`](https://github.com/davesag/amqp-event-tester) — A Dockerised and configurable utility to help integration-test your amqp services. |
93 | 111 |
|
94 | 112 | ## Development
|
95 | 113 |
|
96 | 114 | ### Prerequisites
|
97 | 115 |
|
98 |
| -* [NodeJS](htps://nodejs.org), version 10.15.1 (LTS) or better (I use [`nvm`](https://github.com/creationix/nvm) to manage Node versions — `brew install nvm`.) |
99 |
| -* [Docker](https://www.docker.com) (Use [Docker for Mac](https://docs.docker.com/docker-for-mac/), not the homebrew version) |
| 116 | +- [NodeJS](htps://nodejs.org), version 10.15.3 (LTS) or better (I use [`nvm`](https://github.com/creationix/nvm) to manage Node versions — `brew install nvm`.) |
| 117 | +- [Docker](https://www.docker.com) (Use [Docker for Mac](https://docs.docker.com/docker-for-mac/), not the homebrew version) |
100 | 118 |
|
101 | 119 | ### Initialisation
|
102 | 120 |
|
103 |
| - npm install |
| 121 | +``` |
| 122 | +npm install |
| 123 | +``` |
104 | 124 |
|
105 | 125 | ### To Start the queue server for integration testing.
|
106 | 126 |
|
107 |
| - docker-compose up -d |
| 127 | +``` |
| 128 | +docker-compose up -d |
| 129 | +``` |
108 | 130 |
|
109 | 131 | Runs Rabbit MQ.
|
110 | 132 |
|
111 | 133 | ### Test it
|
112 | 134 |
|
113 |
| -* `npm test` — runs the unit tests (quick and does not need rabbit mq running) |
114 |
| -* `npm run test:integration` — runs the integration tests (not so quick and needs rabbitmq running) |
| 135 | +- `npm test` — runs the unit tests (quick and does not need rabbit mq running) |
| 136 | +- `npm run test:integration` — runs the integration tests (not so quick and needs rabbitmq running) |
115 | 137 |
|
116 | 138 | ### Lint it
|
117 | 139 |
|
118 |
| - npm run lint |
| 140 | +``` |
| 141 | +npm run lint |
| 142 | +``` |
119 | 143 |
|
120 | 144 | ## Contributing
|
121 | 145 |
|
|
0 commit comments