rxjs-rabbitmq 1.0.1 → 1.1.0
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/package.json +1 -1
- package/src/rabbitmq-client.helper.ts +109 -76
- package/src/types.ts +24 -2
package/package.json
CHANGED
|
@@ -46,9 +46,11 @@ import { ContentTypes } from "./enums";
|
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
|
|
49
|
+
export type RmgOnEventHandler = (event: RmqEventMessage, rmqClient?: RabbitMQClient) => void;
|
|
49
50
|
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
/**
|
|
52
|
+
Rabbit MQ - RxJS Powered
|
|
53
|
+
*/
|
|
52
54
|
export class RabbitMQClient {
|
|
53
55
|
private clientInitConfig: RabbitMqInitConfig;
|
|
54
56
|
|
|
@@ -58,6 +60,8 @@ export class RabbitMQClient {
|
|
|
58
60
|
private isReadyStream: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(this.isReady);
|
|
59
61
|
private connectionErrorStream: Subject<any> = new Subject<any>();
|
|
60
62
|
private connectionCloseStream: Subject<any> = new Subject<any>();
|
|
63
|
+
|
|
64
|
+
private messagesStream: Subject<RmqEventMessage> = new Subject<RmqEventMessage>();
|
|
61
65
|
|
|
62
66
|
private queues: MapType<Replies.AssertQueue> = {};
|
|
63
67
|
private exchanges:MapType<Replies.AssertExchange> = {};
|
|
@@ -91,6 +95,10 @@ export class RabbitMQClient {
|
|
|
91
95
|
return this.connectionCloseStream.asObservable();
|
|
92
96
|
}
|
|
93
97
|
|
|
98
|
+
get onMessage(): Observable<RmqEventMessage> {
|
|
99
|
+
return this.messagesStream.asObservable();
|
|
100
|
+
}
|
|
101
|
+
|
|
94
102
|
constructor(clientInitConfig: RabbitMqInitConfig) {
|
|
95
103
|
this.init(clientInitConfig);
|
|
96
104
|
}
|
|
@@ -145,22 +153,20 @@ export class RabbitMQClient {
|
|
|
145
153
|
|
|
146
154
|
queueListenersMap[this.DEFAULT_LISTENER_TYPE] = new ReplaySubject();
|
|
147
155
|
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
156
|
+
for (const messageType of queueConfig.handleMessageTypes) {
|
|
157
|
+
const isStringList = typeof messageType === 'string';
|
|
158
|
+
|
|
159
|
+
if (isStringList) {
|
|
151
160
|
queueListenersMap[messageType as string] = new ReplaySubject();
|
|
152
161
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
for (const messageType of queueConfig.handleMessageTypes) {
|
|
156
|
-
const config: RmqHandleMessageTypeConfig = (messageType as any) as RmqHandleMessageTypeConfig;
|
|
157
|
-
queueCallbacksMap[config.messageType] = config.callbackHandler;
|
|
162
|
+
else {
|
|
163
|
+
queueCallbacksMap[messageType.messageName] = messageType.callbackHandler;
|
|
158
164
|
}
|
|
159
|
-
this.onQueue(queueConfig.name);
|
|
160
165
|
}
|
|
161
166
|
|
|
162
167
|
promises.push(this.channel.assertQueue(queueConfig.name, queueConfig.options));
|
|
163
168
|
}
|
|
169
|
+
|
|
164
170
|
return Promise.all(promises).then((values) => {
|
|
165
171
|
this.queues = mapify(values, 'queue');
|
|
166
172
|
console.log(`queues created on channel`);
|
|
@@ -233,78 +239,89 @@ export class RabbitMQClient {
|
|
|
233
239
|
return this.onReady.pipe(map(() => this.channel));
|
|
234
240
|
}
|
|
235
241
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (!message) {
|
|
243
|
-
throw new Error('Consumer cancelled by server');
|
|
244
|
-
}
|
|
242
|
+
private getQueueListener(queue: string, options?: Options.Consume) {
|
|
243
|
+
return () => {
|
|
244
|
+
const handleCallback = (message: ConsumeMessage | null) => {
|
|
245
|
+
if (!message) {
|
|
246
|
+
throw new Error('Consumer cancelled by server');
|
|
247
|
+
}
|
|
245
248
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
249
|
+
console.log(`received queue message`, message);
|
|
250
|
+
|
|
251
|
+
// see if a listener was created for the routing key
|
|
252
|
+
const messageType = message.properties.type;
|
|
253
|
+
const useContentType = message.properties.contentType;
|
|
254
|
+
const useData = SERIALIZERS[useContentType] ? SERIALIZERS[useContentType].deserialize(message.content) : message.content;
|
|
255
|
+
const messageObj: RmqEventMessage = {
|
|
256
|
+
data: useData,
|
|
257
|
+
message,
|
|
258
|
+
ack: () => {
|
|
259
|
+
this.ack(message);
|
|
260
|
+
},
|
|
261
|
+
sendMessage: this.sendMessage.bind(this),
|
|
262
|
+
sendRequest: this.sendRequest.bind(this),
|
|
263
|
+
publishEvent: this.publishEvent.bind(this),
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
// send message to stream
|
|
267
|
+
this.messagesStream.next(messageObj);
|
|
268
|
+
|
|
269
|
+
// console.log(`Message on queue ${queue}:`, messageObj);
|
|
270
|
+
|
|
271
|
+
if (!messageType) {
|
|
272
|
+
// no type key found, push to default stream
|
|
273
|
+
console.log(`No message type found; pushing to default handler on queue ${queue}`);
|
|
274
|
+
this.queueToEventHandleMapping[queue][this.DEFAULT_LISTENER_TYPE].next(messageObj);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
264
277
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
278
|
+
if (this.queueToEventHandleMapping[queue][messageType]) {
|
|
279
|
+
// there is a registered listener for the type, push to that stream
|
|
280
|
+
console.log(`message type handler found; pushing to ${messageType} handler on queue ${queue}`);
|
|
281
|
+
this.queueToEventHandleMapping[queue][messageType].next(messageObj);
|
|
282
|
+
}
|
|
283
|
+
else if (this.queueToEventCallbackMapping[queue][messageType]) {
|
|
284
|
+
// there is a registered listener for the type, push to that stream
|
|
285
|
+
console.log(`message type callback found; pushing to ${messageType} callback on queue ${queue}`);
|
|
286
|
+
const callbackHandler = this.queueToEventCallbackMapping[queue][messageType];
|
|
287
|
+
callbackHandler(messageObj);
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
// no registered listener
|
|
291
|
+
if (this.clientInitConfig.autoAckUnhandledMessageTypes) {
|
|
292
|
+
console.log(`No handler found for message type ${messageType} on queue ${queue}; auto acknowledging.`);
|
|
293
|
+
this.ack(message);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
else if (this.clientInitConfig.pushUnhandledMessageTypesToDefaultHandler) {
|
|
297
|
+
console.log(`No handler found for message type ${messageType} on queue ${queue}; pushing to ${this.DEFAULT_LISTENER_TYPE} handler.`);
|
|
268
298
|
this.queueToEventHandleMapping[queue][this.DEFAULT_LISTENER_TYPE].next(messageObj);
|
|
299
|
+
return;
|
|
269
300
|
}
|
|
270
301
|
else {
|
|
271
|
-
|
|
272
|
-
if (this.queueToEventHandleMapping[queue][messageType]) {
|
|
273
|
-
// there is a registered listener for the type, push to that stream
|
|
274
|
-
console.log(`message type handler found; pushing to ${messageType} handler on queue ${queue}`);
|
|
275
|
-
this.queueToEventHandleMapping[queue][messageType].next(messageObj);
|
|
276
|
-
}
|
|
277
|
-
else if (this.queueToEventCallbackMapping[queue][messageType]) {
|
|
278
|
-
// there is a registered listener for the type, push to that stream
|
|
279
|
-
console.log(`message type callback found; pushing to ${messageType} callback on queue ${queue}`);
|
|
280
|
-
const callbackHandler = this.queueToEventCallbackMapping[queue][messageType];
|
|
281
|
-
callbackHandler(messageObj);
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
// no registered listener
|
|
285
|
-
if (this.clientInitConfig.autoAckUnhandledMessageTypes) {
|
|
286
|
-
console.log(`No handler found for message type ${messageType} on queue ${queue}; auto acknowledging.`);
|
|
287
|
-
this.ack(message);
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
else if (this.clientInitConfig.pushUnhandledMessageTypesToDefaultHandler) {
|
|
291
|
-
console.log(`No handler found for message type ${messageType} on queue ${queue}; pushing to ${this.DEFAULT_LISTENER_TYPE} handler.`);
|
|
292
|
-
this.queueToEventHandleMapping[queue][this.DEFAULT_LISTENER_TYPE].next(messageObj);
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
else {
|
|
296
|
-
throw new Error(`Message received with unregistered message type handler/callback. Please add message type "${messageType}" in the list of message types for the queue config in the constructor.`);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
302
|
+
throw new Error(`Message received with unregistered message type handler/callback. Please add message type "${messageType}" in the list of message types for the queue config in the constructor.`);
|
|
299
303
|
}
|
|
300
|
-
}
|
|
304
|
+
}
|
|
305
|
+
};
|
|
301
306
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
this.channel.consume(queue, handleCallback, { ...(options || {}), consumerTag: options?.consumerTag || consumerTag });
|
|
305
|
-
}
|
|
307
|
+
const consumerTag = Date.now().toString();
|
|
306
308
|
|
|
307
|
-
this.
|
|
309
|
+
this.channel.consume(queue, handleCallback, { ...(options || {}), consumerTag: options?.consumerTag || consumerTag });
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
onQueue(queue: string, options?: Options.Consume) {
|
|
314
|
+
// listen for messages on the queue
|
|
315
|
+
if (!this.queueListeners[queue]) {
|
|
316
|
+
console.log(`Registering messages/events listener for queue ${queue}:`);
|
|
317
|
+
const startQueueListener = this.getQueueListener(queue, options);
|
|
318
|
+
|
|
319
|
+
this.queueListeners[queue] = this.onReady.pipe(
|
|
320
|
+
tap((readyState) => {
|
|
321
|
+
startQueueListener()
|
|
322
|
+
})
|
|
323
|
+
)
|
|
324
|
+
.subscribe({
|
|
308
325
|
next: (readyState) => {
|
|
309
326
|
console.log(`Registered: Now listening to messages/events on queue ${queue}...`);
|
|
310
327
|
}
|
|
@@ -328,9 +345,25 @@ export class RabbitMQClient {
|
|
|
328
345
|
);
|
|
329
346
|
};
|
|
330
347
|
|
|
348
|
+
const onEvent = (messageType: string, handlerFn: RmgOnEventHandler) => {
|
|
349
|
+
return this.onReady.pipe(
|
|
350
|
+
map((ready: boolean) => {
|
|
351
|
+
if (!this.queueToEventCallbackMapping[queue][messageType]) {
|
|
352
|
+
throw new Error(`The provided routing key was not provided during initialization. Please add routing key "${messageType}" in the list of routing keys for the queue config in the constructor.`);
|
|
353
|
+
}
|
|
354
|
+
return this.queueToEventHandleMapping[queue][messageType].asObservable().subscribe({
|
|
355
|
+
next: (event) => {
|
|
356
|
+
handlerFn(event, this);
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
})
|
|
360
|
+
);
|
|
361
|
+
};
|
|
362
|
+
|
|
331
363
|
return {
|
|
332
364
|
handle,
|
|
333
|
-
handleDefault
|
|
365
|
+
handleDefault,
|
|
366
|
+
onEvent
|
|
334
367
|
};
|
|
335
368
|
}
|
|
336
369
|
|
package/src/types.ts
CHANGED
|
@@ -4,16 +4,34 @@ import {
|
|
|
4
4
|
Message
|
|
5
5
|
} from "amqplib";
|
|
6
6
|
|
|
7
|
+
/*
|
|
8
|
+
|
|
9
|
+
*/
|
|
7
10
|
|
|
8
11
|
|
|
9
12
|
export type MapType<T = any> = { [key:string]: T };
|
|
10
13
|
|
|
11
14
|
|
|
12
15
|
export type RmqMessageTypeHandler = (event: RmqEventMessage) => Promise<void>;
|
|
13
|
-
export type RmqHandleMessageTypeConfig = {
|
|
16
|
+
export type RmqHandleMessageTypeConfig = { messageName: string, callbackHandler: RmqMessageTypeHandler };
|
|
14
17
|
export type RmqHandleMessageTypeConfigs = string[] | RmqHandleMessageTypeConfig[];
|
|
15
18
|
|
|
16
|
-
export interface QueueConfig {
|
|
19
|
+
export interface QueueConfig {
|
|
20
|
+
/*
|
|
21
|
+
The name of the queue you want to consume messages from
|
|
22
|
+
*/
|
|
23
|
+
name: string,
|
|
24
|
+
|
|
25
|
+
/*
|
|
26
|
+
the event names
|
|
27
|
+
*/
|
|
28
|
+
handleMessageTypes: RmqHandleMessageTypeConfigs,
|
|
29
|
+
|
|
30
|
+
/*
|
|
31
|
+
|
|
32
|
+
*/
|
|
33
|
+
options?: Options.AssertQueue
|
|
34
|
+
}
|
|
17
35
|
export interface ExchangeConfig { name: string, type: string, options?: Options.AssertExchange }
|
|
18
36
|
export interface QueueExchangeBindingConfig { queue: string, exchange: string, routingKey: string }
|
|
19
37
|
|
|
@@ -58,6 +76,10 @@ export type RabbitMqInitConfig = {
|
|
|
58
76
|
retryAttempts: number,
|
|
59
77
|
retryDelay: number
|
|
60
78
|
prefetch?: number,
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
The queues you want to consume on this client
|
|
82
|
+
*/
|
|
61
83
|
queues: Array<QueueConfig>,
|
|
62
84
|
exchanges: Array<ExchangeConfig>,
|
|
63
85
|
bindings: Array<QueueExchangeBindingConfig>,
|