@appinventiv/rabbit-mq 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +168 -122
- package/dist/consumer/consumer.rabbitmq.d.ts +18 -12
- package/dist/consumer/consumer.rabbitmq.js +53 -34
- package/dist/consumer/consumer.rabbitmq.js.map +1 -1
- package/dist/consumer/consumer.service.d.ts +22 -9
- package/dist/consumer/consumer.service.js +32 -18
- package/dist/consumer/consumer.service.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/interface.d.ts +7 -1
- package/dist/manager-key.d.ts +11 -0
- package/dist/manager-key.js +23 -0
- package/dist/manager-key.js.map +1 -0
- package/dist/producer/producer.rabbitmq.d.ts +19 -12
- package/dist/producer/producer.rabbitmq.js +37 -22
- package/dist/producer/producer.rabbitmq.js.map +1 -1
- package/dist/producer/producer.service.d.ts +36 -16
- package/dist/producer/producer.service.js +51 -21
- package/dist/producer/producer.service.js.map +1 -1
- package/dist/rabbitmq.d.ts +17 -2
- package/dist/rabbitmq.js +38 -10
- package/dist/rabbitmq.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/consumer/consumer.rabbitmq.ts +56 -35
- package/src/consumer/consumer.service.ts +30 -19
- package/src/index.ts +3 -1
- package/src/interface.ts +10 -2
- package/src/manager-key.ts +21 -0
- package/src/producer/producer.rabbitmq.ts +43 -27
- package/src/producer/producer.service.ts +60 -21
- package/src/rabbitmq.ts +46 -10
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@ npm install @developer-at/rabbit-mq
|
|
|
12
12
|
|
|
13
13
|
- Producer service for publishing messages
|
|
14
14
|
- Consumer service for consuming messages
|
|
15
|
+
- Default `rabbitMQ` singleton with optional custom `RabbitMQManager` instances
|
|
15
16
|
- Automatic connection management
|
|
16
17
|
- Queue and exchange creation
|
|
17
18
|
- Message acknowledgment handling
|
|
@@ -27,22 +28,60 @@ npm install @developer-at/rabbit-mq
|
|
|
27
28
|
|
|
28
29
|
### Basic Setup
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
Configure RabbitMQ **once** at startup. All producers and consumers use this config automatically.
|
|
31
32
|
|
|
32
33
|
```typescript
|
|
33
|
-
import {
|
|
34
|
+
import { rabbitMQ, producer, consumer } from '@developer-at/rabbit-mq';
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
const manager = new RabbitMQManager();
|
|
37
|
-
manager.setConfig({
|
|
36
|
+
rabbitMQ.setConfig({
|
|
38
37
|
url: 'amqp://localhost:5672',
|
|
39
38
|
connectionOptions: {
|
|
40
39
|
// Optional connection options
|
|
41
40
|
}
|
|
42
41
|
});
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
await producer.produce('my-queue', { event: 'created' });
|
|
44
|
+
|
|
45
|
+
await consumer.consume({
|
|
46
|
+
queue: 'my-queue',
|
|
47
|
+
onMessage: async (message) => {
|
|
48
|
+
console.log('Received:', message);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
For queue/exchange setup with a dedicated manager instance:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { rabbitMQ } from '@developer-at/rabbit-mq';
|
|
57
|
+
|
|
58
|
+
await rabbitMQ.connect();
|
|
59
|
+
await rabbitMQ.createQueue({ name: 'user-events', durable: true });
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Alternate broker (optional)
|
|
63
|
+
|
|
64
|
+
Pass a custom `RabbitMQManager` when you need a different URL:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { rabbitMQ, producer } from '@developer-at/rabbit-mq';
|
|
68
|
+
|
|
69
|
+
const analyticsMq = new RabbitMQManager();
|
|
70
|
+
analyticsMq.setConfig({ url: process.env.ANALYTICS_RABBIT_URL! });
|
|
71
|
+
|
|
72
|
+
await producer.produce('events', { id: 1 }, undefined, undefined, analyticsMq);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Or pass `rabbitMq` on consumer options: `consumer.consume({ queue: 'events', onMessage, rabbitMq: analyticsMq })`.
|
|
76
|
+
|
|
77
|
+
### Shutdown
|
|
78
|
+
|
|
79
|
+
`producer.disconnectAll()` and `consumer.disconnectConsumers()` do **not** close the default `rabbitMQ` connection. On app shutdown:
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
await producer.disconnectAll();
|
|
83
|
+
await consumer.disconnectConsumers();
|
|
84
|
+
await rabbitMQ.disconnect();
|
|
46
85
|
```
|
|
47
86
|
|
|
48
87
|
### Queue and Exchange Management
|
|
@@ -52,14 +91,13 @@ await manager.connect();
|
|
|
52
91
|
Use the `createQueue` method from `RabbitMQManager` to create queues before using them:
|
|
53
92
|
|
|
54
93
|
```typescript
|
|
55
|
-
import {
|
|
94
|
+
import { rabbitMQ } from '@developer-at/rabbit-mq';
|
|
56
95
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
await manager.connect();
|
|
96
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
97
|
+
await rabbitMQ.connect();
|
|
60
98
|
|
|
61
99
|
// Create a durable queue (survives broker restart)
|
|
62
|
-
await
|
|
100
|
+
await rabbitMQ.createQueue({
|
|
63
101
|
name: 'user-events',
|
|
64
102
|
durable: true, // Queue persists after broker restart
|
|
65
103
|
exclusive: false, // Queue can be accessed by multiple connections
|
|
@@ -67,7 +105,7 @@ await manager.createQueue({
|
|
|
67
105
|
});
|
|
68
106
|
|
|
69
107
|
// Create a temporary queue (deleted when connection closes)
|
|
70
|
-
await
|
|
108
|
+
await rabbitMQ.createQueue({
|
|
71
109
|
name: 'temp-queue',
|
|
72
110
|
durable: false,
|
|
73
111
|
exclusive: true, // Queue is exclusive to this connection
|
|
@@ -80,14 +118,13 @@ await manager.createQueue({
|
|
|
80
118
|
Use the `createExchange` method to create exchanges for message routing:
|
|
81
119
|
|
|
82
120
|
```typescript
|
|
83
|
-
import {
|
|
121
|
+
import { rabbitMQ } from '@developer-at/rabbit-mq';
|
|
84
122
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
await manager.connect();
|
|
123
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
124
|
+
await rabbitMQ.connect();
|
|
88
125
|
|
|
89
126
|
// Create a direct exchange (routes messages based on exact routing key match)
|
|
90
|
-
await
|
|
127
|
+
await rabbitMQ.createExchange({
|
|
91
128
|
name: 'user-exchange',
|
|
92
129
|
type: 'direct', // Options: 'direct', 'topic', 'fanout', 'headers'
|
|
93
130
|
durable: true, // Exchange persists after broker restart
|
|
@@ -95,14 +132,14 @@ await manager.createExchange({
|
|
|
95
132
|
});
|
|
96
133
|
|
|
97
134
|
// Create a topic exchange (routes messages based on pattern matching)
|
|
98
|
-
await
|
|
135
|
+
await rabbitMQ.createExchange({
|
|
99
136
|
name: 'notifications',
|
|
100
137
|
type: 'topic',
|
|
101
138
|
durable: true
|
|
102
139
|
});
|
|
103
140
|
|
|
104
141
|
// Create a fanout exchange (broadcasts to all bound queues)
|
|
105
|
-
await
|
|
142
|
+
await rabbitMQ.createExchange({
|
|
106
143
|
name: 'broadcast',
|
|
107
144
|
type: 'fanout',
|
|
108
145
|
durable: true
|
|
@@ -114,20 +151,19 @@ await manager.createExchange({
|
|
|
114
151
|
Use `bindQueue` to connect queues to exchanges with routing keys. The routing key determines which messages from the exchange are delivered to the queue:
|
|
115
152
|
|
|
116
153
|
```typescript
|
|
117
|
-
import {
|
|
154
|
+
import { rabbitMQ } from '@developer-at/rabbit-mq';
|
|
118
155
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
await manager.connect();
|
|
156
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
157
|
+
await rabbitMQ.connect();
|
|
122
158
|
|
|
123
159
|
// Create exchange and queue first
|
|
124
|
-
await
|
|
160
|
+
await rabbitMQ.createExchange({
|
|
125
161
|
name: 'user-exchange',
|
|
126
162
|
type: 'direct',
|
|
127
163
|
durable: true
|
|
128
164
|
});
|
|
129
165
|
|
|
130
|
-
await
|
|
166
|
+
await rabbitMQ.createQueue({
|
|
131
167
|
name: 'user-created-queue',
|
|
132
168
|
durable: true
|
|
133
169
|
});
|
|
@@ -135,15 +171,15 @@ await manager.createQueue({
|
|
|
135
171
|
// Bind queue to exchange with routing key
|
|
136
172
|
// Messages published to 'user-exchange' with routing key 'user.created'
|
|
137
173
|
// will be routed to 'user-created-queue'
|
|
138
|
-
await
|
|
174
|
+
await rabbitMQ.bindQueue('user-created-queue', 'user-exchange', 'user.created');
|
|
139
175
|
|
|
140
176
|
// You can bind the same queue to multiple routing keys
|
|
141
|
-
await
|
|
177
|
+
await rabbitMQ.bindQueue('user-created-queue', 'user-exchange', 'user.updated');
|
|
142
178
|
|
|
143
179
|
// For fanout exchanges, routing key is ignored (all messages go to all bound queues)
|
|
144
|
-
await
|
|
145
|
-
await
|
|
146
|
-
await
|
|
180
|
+
await rabbitMQ.createExchange({ name: 'broadcast', type: 'fanout', durable: true });
|
|
181
|
+
await rabbitMQ.bindQueue('queue1', 'broadcast', ''); // routing key ignored for fanout
|
|
182
|
+
await rabbitMQ.bindQueue('queue2', 'broadcast', ''); // routing key ignored for fanout
|
|
147
183
|
```
|
|
148
184
|
|
|
149
185
|
**How Routing Works:**
|
|
@@ -165,15 +201,14 @@ await manager.bindQueue('queue2', 'broadcast', ''); // routing key ignored for f
|
|
|
165
201
|
When you don't specify an exchange, messages are sent directly to the queue:
|
|
166
202
|
|
|
167
203
|
```typescript
|
|
168
|
-
import {
|
|
204
|
+
import { rabbitMQ, producer } from '@developer-at/rabbit-mq';
|
|
169
205
|
|
|
170
206
|
// Setup connection
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
await manager.connect();
|
|
207
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
208
|
+
await rabbitMQ.connect();
|
|
174
209
|
|
|
175
210
|
// Create queue first (optional, but recommended)
|
|
176
|
-
await
|
|
211
|
+
await rabbitMQ.createQueue({
|
|
177
212
|
name: 'user-events',
|
|
178
213
|
durable: true
|
|
179
214
|
});
|
|
@@ -191,27 +226,26 @@ await producer.produce('user-events', {
|
|
|
191
226
|
When using an exchange, you must specify both the exchange name and routing key. The routing key determines which bound queues receive the message:
|
|
192
227
|
|
|
193
228
|
```typescript
|
|
194
|
-
import {
|
|
229
|
+
import { rabbitMQ, producer } from '@developer-at/rabbit-mq';
|
|
195
230
|
|
|
196
231
|
// Setup connection
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
await manager.connect();
|
|
232
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
233
|
+
await rabbitMQ.connect();
|
|
200
234
|
|
|
201
235
|
// Create exchange
|
|
202
|
-
await
|
|
236
|
+
await rabbitMQ.createExchange({
|
|
203
237
|
name: 'user-exchange',
|
|
204
238
|
type: 'direct',
|
|
205
239
|
durable: true
|
|
206
240
|
});
|
|
207
241
|
|
|
208
242
|
// Create queues
|
|
209
|
-
await
|
|
210
|
-
await
|
|
243
|
+
await rabbitMQ.createQueue({ name: 'user-created-queue', durable: true });
|
|
244
|
+
await rabbitMQ.createQueue({ name: 'user-updated-queue', durable: true });
|
|
211
245
|
|
|
212
246
|
// Bind queues to exchange with different routing keys
|
|
213
|
-
await
|
|
214
|
-
await
|
|
247
|
+
await rabbitMQ.bindQueue('user-created-queue', 'user-exchange', 'user.created');
|
|
248
|
+
await rabbitMQ.bindQueue('user-updated-queue', 'user-exchange', 'user.updated');
|
|
215
249
|
|
|
216
250
|
// Publish message to exchange with routing key 'user.created'
|
|
217
251
|
// This message will be routed to 'user-created-queue' only
|
|
@@ -235,28 +269,27 @@ await producer.produce(
|
|
|
235
269
|
#### Topic Exchange Example with Pattern Matching
|
|
236
270
|
|
|
237
271
|
```typescript
|
|
238
|
-
import {
|
|
272
|
+
import { rabbitMQ, producer } from '@developer-at/rabbit-mq';
|
|
239
273
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
await manager.connect();
|
|
274
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
275
|
+
await rabbitMQ.connect();
|
|
243
276
|
|
|
244
277
|
// Create topic exchange
|
|
245
|
-
await
|
|
278
|
+
await rabbitMQ.createExchange({
|
|
246
279
|
name: 'notifications',
|
|
247
280
|
type: 'topic',
|
|
248
281
|
durable: true
|
|
249
282
|
});
|
|
250
283
|
|
|
251
284
|
// Create queues
|
|
252
|
-
await
|
|
253
|
-
await
|
|
254
|
-
await
|
|
285
|
+
await rabbitMQ.createQueue({ name: 'email-queue', durable: true });
|
|
286
|
+
await rabbitMQ.createQueue({ name: 'sms-queue', durable: true });
|
|
287
|
+
await rabbitMQ.createQueue({ name: 'all-notifications-queue', durable: true });
|
|
255
288
|
|
|
256
289
|
// Bind with pattern matching
|
|
257
|
-
await
|
|
258
|
-
await
|
|
259
|
-
await
|
|
290
|
+
await rabbitMQ.bindQueue('email-queue', 'notifications', 'notification.email.*');
|
|
291
|
+
await rabbitMQ.bindQueue('sms-queue', 'notifications', 'notification.sms.*');
|
|
292
|
+
await rabbitMQ.bindQueue('all-notifications-queue', 'notifications', 'notification.#');
|
|
260
293
|
|
|
261
294
|
// Publish to 'notification.email.user' - goes to email-queue and all-notifications-queue
|
|
262
295
|
await producer.produce(
|
|
@@ -278,28 +311,27 @@ await producer.produce(
|
|
|
278
311
|
#### Fanout Exchange Example (Broadcast)
|
|
279
312
|
|
|
280
313
|
```typescript
|
|
281
|
-
import {
|
|
314
|
+
import { rabbitMQ, producer } from '@developer-at/rabbit-mq';
|
|
282
315
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
await manager.connect();
|
|
316
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
317
|
+
await rabbitMQ.connect();
|
|
286
318
|
|
|
287
319
|
// Create fanout exchange
|
|
288
|
-
await
|
|
320
|
+
await rabbitMQ.createExchange({
|
|
289
321
|
name: 'broadcast',
|
|
290
322
|
type: 'fanout',
|
|
291
323
|
durable: true
|
|
292
324
|
});
|
|
293
325
|
|
|
294
326
|
// Create multiple queues
|
|
295
|
-
await
|
|
296
|
-
await
|
|
297
|
-
await
|
|
327
|
+
await rabbitMQ.createQueue({ name: 'queue1', durable: true });
|
|
328
|
+
await rabbitMQ.createQueue({ name: 'queue2', durable: true });
|
|
329
|
+
await rabbitMQ.createQueue({ name: 'queue3', durable: true });
|
|
298
330
|
|
|
299
331
|
// Bind all queues to fanout exchange (routing key is ignored)
|
|
300
|
-
await
|
|
301
|
-
await
|
|
302
|
-
await
|
|
332
|
+
await rabbitMQ.bindQueue('queue1', 'broadcast', '');
|
|
333
|
+
await rabbitMQ.bindQueue('queue2', 'broadcast', '');
|
|
334
|
+
await rabbitMQ.bindQueue('queue3', 'broadcast', '');
|
|
303
335
|
|
|
304
336
|
// Publish message - ALL queues receive it (routing key is ignored for fanout)
|
|
305
337
|
await producer.produce(
|
|
@@ -317,15 +349,14 @@ await producer.produce(
|
|
|
317
349
|
When consuming from a queue without an exchange, messages are consumed directly from the queue:
|
|
318
350
|
|
|
319
351
|
```typescript
|
|
320
|
-
import {
|
|
352
|
+
import { rabbitMQ, consumer } from '@developer-at/rabbit-mq';
|
|
321
353
|
|
|
322
354
|
// Setup connection
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
await manager.connect();
|
|
355
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
356
|
+
await rabbitMQ.connect();
|
|
326
357
|
|
|
327
358
|
// Create queue first (optional, but recommended)
|
|
328
|
-
await
|
|
359
|
+
await rabbitMQ.createQueue({
|
|
329
360
|
name: 'user-events',
|
|
330
361
|
durable: true
|
|
331
362
|
});
|
|
@@ -351,28 +382,27 @@ await consumer.consume({
|
|
|
351
382
|
When consuming from an exchange-based setup, you need to specify the exchange and routing key that the queue is bound to:
|
|
352
383
|
|
|
353
384
|
```typescript
|
|
354
|
-
import {
|
|
385
|
+
import { rabbitMQ, consumer } from '@developer-at/rabbit-mq';
|
|
355
386
|
|
|
356
387
|
// Setup connection
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
await manager.connect();
|
|
388
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
389
|
+
await rabbitMQ.connect();
|
|
360
390
|
|
|
361
391
|
// Create exchange
|
|
362
|
-
await
|
|
392
|
+
await rabbitMQ.createExchange({
|
|
363
393
|
name: 'user-exchange',
|
|
364
394
|
type: 'direct',
|
|
365
395
|
durable: true
|
|
366
396
|
});
|
|
367
397
|
|
|
368
398
|
// Create queue
|
|
369
|
-
await
|
|
399
|
+
await rabbitMQ.createQueue({
|
|
370
400
|
name: 'user-created-queue',
|
|
371
401
|
durable: true
|
|
372
402
|
});
|
|
373
403
|
|
|
374
404
|
// Bind queue to exchange with routing key
|
|
375
|
-
await
|
|
405
|
+
await rabbitMQ.bindQueue('user-created-queue', 'user-exchange', 'user.created');
|
|
376
406
|
|
|
377
407
|
// Consume from queue that's bound to exchange
|
|
378
408
|
// You must specify the exchange and routing key that the queue is bound to
|
|
@@ -392,28 +422,27 @@ await consumer.consume({
|
|
|
392
422
|
#### Multiple Consumers with Different Routing Keys
|
|
393
423
|
|
|
394
424
|
```typescript
|
|
395
|
-
import {
|
|
425
|
+
import { rabbitMQ, consumer } from '@developer-at/rabbit-mq';
|
|
396
426
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
await manager.connect();
|
|
427
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
428
|
+
await rabbitMQ.connect();
|
|
400
429
|
|
|
401
430
|
// Setup exchange
|
|
402
|
-
await
|
|
431
|
+
await rabbitMQ.createExchange({
|
|
403
432
|
name: 'user-exchange',
|
|
404
433
|
type: 'direct',
|
|
405
434
|
durable: true
|
|
406
435
|
});
|
|
407
436
|
|
|
408
437
|
// Create queues for different events
|
|
409
|
-
await
|
|
410
|
-
await
|
|
411
|
-
await
|
|
438
|
+
await rabbitMQ.createQueue({ name: 'user-created-queue', durable: true });
|
|
439
|
+
await rabbitMQ.createQueue({ name: 'user-updated-queue', durable: true });
|
|
440
|
+
await rabbitMQ.createQueue({ name: 'user-deleted-queue', durable: true });
|
|
412
441
|
|
|
413
442
|
// Bind queues with different routing keys
|
|
414
|
-
await
|
|
415
|
-
await
|
|
416
|
-
await
|
|
443
|
+
await rabbitMQ.bindQueue('user-created-queue', 'user-exchange', 'user.created');
|
|
444
|
+
await rabbitMQ.bindQueue('user-updated-queue', 'user-exchange', 'user.updated');
|
|
445
|
+
await rabbitMQ.bindQueue('user-deleted-queue', 'user-exchange', 'user.deleted');
|
|
417
446
|
|
|
418
447
|
// Consumer for user.created events
|
|
419
448
|
await consumer.consume({
|
|
@@ -449,24 +478,23 @@ await consumer.consume({
|
|
|
449
478
|
#### Topic Exchange Consumer with Pattern Matching
|
|
450
479
|
|
|
451
480
|
```typescript
|
|
452
|
-
import {
|
|
481
|
+
import { rabbitMQ, consumer } from '@developer-at/rabbit-mq';
|
|
453
482
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
await manager.connect();
|
|
483
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
484
|
+
await rabbitMQ.connect();
|
|
457
485
|
|
|
458
486
|
// Create topic exchange
|
|
459
|
-
await
|
|
487
|
+
await rabbitMQ.createExchange({
|
|
460
488
|
name: 'notifications',
|
|
461
489
|
type: 'topic',
|
|
462
490
|
durable: true
|
|
463
491
|
});
|
|
464
492
|
|
|
465
493
|
// Create queue for email notifications
|
|
466
|
-
await
|
|
494
|
+
await rabbitMQ.createQueue({ name: 'email-notifications', durable: true });
|
|
467
495
|
|
|
468
496
|
// Bind with pattern - receives all email.* notifications
|
|
469
|
-
await
|
|
497
|
+
await rabbitMQ.bindQueue('email-notifications', 'notifications', 'notification.email.*');
|
|
470
498
|
|
|
471
499
|
// Consumer receives messages matching the pattern
|
|
472
500
|
await consumer.consume({
|
|
@@ -485,19 +513,17 @@ await consumer.consume({
|
|
|
485
513
|
#### Example 1: Simple Queue (No Exchange)
|
|
486
514
|
|
|
487
515
|
```typescript
|
|
488
|
-
import {
|
|
516
|
+
import { rabbitMQ, producer, consumer } from '@developer-at/rabbit-mq';
|
|
489
517
|
|
|
490
518
|
async function setupSimpleQueue() {
|
|
491
|
-
|
|
492
|
-
const manager = new RabbitMQManager();
|
|
493
|
-
manager.setConfig({
|
|
519
|
+
rabbitMQ.setConfig({
|
|
494
520
|
url: process.env.RABBITMQ_URL || 'amqp://localhost:5672'
|
|
495
521
|
});
|
|
496
|
-
|
|
497
|
-
await
|
|
522
|
+
|
|
523
|
+
await rabbitMQ.connect();
|
|
498
524
|
|
|
499
525
|
// Create queue
|
|
500
|
-
await
|
|
526
|
+
await rabbitMQ.createQueue({
|
|
501
527
|
name: 'user-events',
|
|
502
528
|
durable: true
|
|
503
529
|
});
|
|
@@ -528,30 +554,29 @@ async function setupSimpleQueue() {
|
|
|
528
554
|
#### Example 2: Exchange-Based with Routing Keys
|
|
529
555
|
|
|
530
556
|
```typescript
|
|
531
|
-
import {
|
|
557
|
+
import { rabbitMQ, producer, consumer } from '@developer-at/rabbit-mq';
|
|
532
558
|
|
|
533
559
|
async function setupExchangeBased() {
|
|
534
|
-
|
|
535
|
-
manager.setConfig({
|
|
560
|
+
rabbitMQ.setConfig({
|
|
536
561
|
url: process.env.RABBITMQ_URL || 'amqp://localhost:5672'
|
|
537
562
|
});
|
|
538
|
-
|
|
539
|
-
await
|
|
563
|
+
|
|
564
|
+
await rabbitMQ.connect();
|
|
540
565
|
|
|
541
566
|
// Create exchange
|
|
542
|
-
await
|
|
567
|
+
await rabbitMQ.createExchange({
|
|
543
568
|
name: 'user-exchange',
|
|
544
569
|
type: 'direct',
|
|
545
570
|
durable: true
|
|
546
571
|
});
|
|
547
572
|
|
|
548
573
|
// Create queues
|
|
549
|
-
await
|
|
550
|
-
await
|
|
574
|
+
await rabbitMQ.createQueue({ name: 'user-created-queue', durable: true });
|
|
575
|
+
await rabbitMQ.createQueue({ name: 'user-updated-queue', durable: true });
|
|
551
576
|
|
|
552
577
|
// Bind queues to exchange with routing keys
|
|
553
|
-
await
|
|
554
|
-
await
|
|
578
|
+
await rabbitMQ.bindQueue('user-created-queue', 'user-exchange', 'user.created');
|
|
579
|
+
await rabbitMQ.bindQueue('user-updated-queue', 'user-exchange', 'user.updated');
|
|
555
580
|
|
|
556
581
|
// Producer - publish to exchange with routing key
|
|
557
582
|
await producer.produce(
|
|
@@ -582,25 +607,46 @@ async function setupExchangeBased() {
|
|
|
582
607
|
process.on('SIGTERM', async () => {
|
|
583
608
|
await producer.disconnectAll();
|
|
584
609
|
await consumer.disconnectConsumers();
|
|
610
|
+
await rabbitMQ.disconnect();
|
|
585
611
|
process.exit(0);
|
|
586
612
|
});
|
|
587
613
|
```
|
|
588
614
|
|
|
589
615
|
## API Reference
|
|
590
616
|
|
|
617
|
+
### `rabbitMQ` (singleton)
|
|
618
|
+
|
|
619
|
+
Default shared `RabbitMQManager` instance. Configure once:
|
|
620
|
+
|
|
621
|
+
```typescript
|
|
622
|
+
import { rabbitMQ } from '@developer-at/rabbit-mq';
|
|
623
|
+
|
|
624
|
+
rabbitMQ.setConfig({ url: 'amqp://localhost:5672' });
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
Producer and consumer services use `rabbitMQ` unless you pass another manager.
|
|
628
|
+
|
|
591
629
|
### RabbitMQManager
|
|
592
630
|
|
|
593
|
-
|
|
631
|
+
Connection and channel management. Use `new RabbitMQManager()` for additional brokers.
|
|
594
632
|
|
|
595
633
|
#### `setConfig(config: IRabbitMQConfig)`
|
|
596
634
|
|
|
597
|
-
Sets
|
|
635
|
+
Sets connection configuration on **this** instance.
|
|
598
636
|
|
|
599
637
|
**Parameters:**
|
|
600
638
|
|
|
601
639
|
- `config.url` (string): RabbitMQ connection URL
|
|
602
640
|
- `config.connectionOptions` (any, optional): Additional connection options
|
|
603
641
|
|
|
642
|
+
#### `isConfigured(): boolean`
|
|
643
|
+
|
|
644
|
+
Returns whether `setConfig` has been called on this instance.
|
|
645
|
+
|
|
646
|
+
#### `getUrl(): string | undefined`
|
|
647
|
+
|
|
648
|
+
Returns the configured URL (useful for debugging).
|
|
649
|
+
|
|
604
650
|
#### `connect()`
|
|
605
651
|
|
|
606
652
|
Establishes connection to RabbitMQ.
|
|
@@ -624,7 +670,7 @@ Creates a queue if it doesn't exist. This method should be called after connecti
|
|
|
624
670
|
**Example:**
|
|
625
671
|
|
|
626
672
|
```typescript
|
|
627
|
-
await
|
|
673
|
+
await rabbitMQ.createQueue({
|
|
628
674
|
name: 'my-queue',
|
|
629
675
|
durable: true,
|
|
630
676
|
exclusive: false,
|
|
@@ -651,7 +697,7 @@ Creates an exchange if it doesn't exist. This method should be called after conn
|
|
|
651
697
|
**Example:**
|
|
652
698
|
|
|
653
699
|
```typescript
|
|
654
|
-
await
|
|
700
|
+
await rabbitMQ.createExchange({
|
|
655
701
|
name: 'my-exchange',
|
|
656
702
|
type: 'direct',
|
|
657
703
|
durable: true
|
|
@@ -679,14 +725,14 @@ Binds a queue to an exchange with an optional routing key. The routing key deter
|
|
|
679
725
|
|
|
680
726
|
```typescript
|
|
681
727
|
// Direct exchange - exact match
|
|
682
|
-
await
|
|
728
|
+
await rabbitMQ.bindQueue('queue1', 'exchange1', 'user.created');
|
|
683
729
|
|
|
684
730
|
// Topic exchange - pattern matching
|
|
685
|
-
await
|
|
686
|
-
await
|
|
731
|
+
await rabbitMQ.bindQueue('queue2', 'exchange2', 'user.*'); // Matches user.created, user.updated, etc.
|
|
732
|
+
await rabbitMQ.bindQueue('queue3', 'exchange2', 'user.#'); // Matches user.created, user.profile.updated, etc.
|
|
687
733
|
|
|
688
734
|
// Fanout exchange - routing key ignored
|
|
689
|
-
await
|
|
735
|
+
await rabbitMQ.bindQueue('queue4', 'exchange3', ''); // All messages received
|
|
690
736
|
```
|
|
691
737
|
|
|
692
738
|
### Producer Service
|
|
@@ -2,42 +2,48 @@ import { IRabbitMQConsumer, IRabbitMQConsumerOptions } from '../interface';
|
|
|
2
2
|
import { RabbitMQManager } from '../rabbitmq';
|
|
3
3
|
/**
|
|
4
4
|
* @description RabbitMQ consumer class for consuming messages from queues
|
|
5
|
-
*
|
|
5
|
+
* Uses a composed RabbitMQManager instance (default: package singleton `rabbitMQ`)
|
|
6
|
+
* Provides message consumption with automatic acknowledgment
|
|
6
7
|
*/
|
|
7
|
-
export declare class RabbitMQConsumer
|
|
8
|
+
export declare class RabbitMQConsumer implements IRabbitMQConsumer {
|
|
9
|
+
private readonly rabbitMq;
|
|
8
10
|
private readonly queue;
|
|
9
11
|
private readonly exchange?;
|
|
10
12
|
private readonly routingKey?;
|
|
11
13
|
private readonly onMessage;
|
|
12
14
|
private readonly prefetchCount;
|
|
13
|
-
private readonly durable;
|
|
14
|
-
private readonly exclusive;
|
|
15
|
-
private readonly autoDelete;
|
|
16
15
|
private consumerTag?;
|
|
17
16
|
/**
|
|
18
17
|
* @description Initialize RabbitMQ consumer with configuration
|
|
19
|
-
* @param {IRabbitMQConsumerOptions} options - Consumer configuration options
|
|
18
|
+
* @param {IRabbitMQConsumerOptions} options - Consumer configuration options (queue, onMessage, optional `rabbitMq`)
|
|
20
19
|
*/
|
|
21
20
|
constructor(options: IRabbitMQConsumerOptions);
|
|
22
21
|
/**
|
|
23
|
-
* @description Consume
|
|
24
|
-
*
|
|
22
|
+
* @description Consume messages from the configured queue
|
|
23
|
+
* Invokes `onMessage` for each message; ack on success, nack without requeue on failure
|
|
25
24
|
*/
|
|
26
25
|
consumeEachMessage(): Promise<void>;
|
|
27
26
|
/**
|
|
28
|
-
* @description Open
|
|
27
|
+
* @description Open connection (via manager) and start consuming
|
|
29
28
|
*/
|
|
30
29
|
connect(): Promise<void>;
|
|
31
30
|
/**
|
|
32
|
-
* @description
|
|
31
|
+
* @description Cancel consumer and optionally disconnect dedicated manager
|
|
32
|
+
* Does not close the default `rabbitMQ` singleton connection
|
|
33
33
|
*/
|
|
34
34
|
disconnect(): Promise<void>;
|
|
35
35
|
/**
|
|
36
|
-
* @description Register event handlers
|
|
36
|
+
* @description Register connection and channel event handlers
|
|
37
37
|
*/
|
|
38
38
|
registerEvents(): Promise<void>;
|
|
39
39
|
/**
|
|
40
|
-
* @description Get consumer tag
|
|
40
|
+
* @description Get consumer tag assigned by RabbitMQ
|
|
41
|
+
* @returns {string | undefined} Consumer tag when consuming is active
|
|
41
42
|
*/
|
|
42
43
|
getConsumerTag(): string | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* @description Get the RabbitMQ manager instance used by this consumer
|
|
46
|
+
* @returns {RabbitMQManager} Composed manager
|
|
47
|
+
*/
|
|
48
|
+
getRabbitMq(): RabbitMQManager;
|
|
43
49
|
}
|