1
- # First of all
1
+ # NestJS RabbitMQ
2
2
3
- ** This package is primarily intended for internal, private use in own projects. If it meets your needs, feel free to use it, but in case of any modification requests, I will consider my own needs first.**
3
+ ** THIS PACKAGE IS PRIMARILY INTENDED FOR INTERNAL/PRIVATE USE IN OWN PROJECTS.
4
+ IF IT MEETS YOUR NEEDS, FEEL FREE TO USE IT, BUT IN CASE OF ANY MODIFICATION REQUESTS, I WILL CONSIDER MY OWN NEEDS FIRST.**
4
5
5
- # NestJS RabbitMQ
6
+ The package is part of the [ rabbitmq-multiverse ] ( https://github.com/djereg/rabbitmq-multiverse ) .
6
7
7
- ## Table of Contents
8
+ # Table of Contents
8
9
9
10
- [ Description] ( #description )
10
11
- [ Motivation] ( #motivation )
13
14
- [ Configuration] ( #configuration )
14
15
- [ Module Initialization] ( #module-initialization )
15
16
- [ Events] ( #events )
16
- - [ Emitting Event] ( #emitting-event )
17
- - [ Subscribing to Event] ( #subscribing-to-event )
17
+ - [ Emitting events] ( #emitting-events )
18
+ - [ Listening to events] ( #listening-to-events )
19
+ - [ Subscribing to events] ( #subscribing-to-events )
18
20
- [ RPC] ( #rpc )
19
21
- [ Setup] ( #setup )
20
- - [ Method Call] ( #method-call )
22
+ - [ Calling remote procedures] ( #calling-remote-procedures )
23
+ - [ Registering remote procedures] ( #registering-remote-procedures )
21
24
- [ Notification] ( #notification )
22
25
- [ Batch Call] ( #batch-call )
26
+ - [ Lifecycle events] ( #lifecycle-events )
27
+ - [ MessagePublishing] ( #messagepublishing )
28
+ - [ MessageProcessing] ( #messageprocessing )
29
+ - [ MessageProcessed] ( #messageprocessed )
30
+ - [ License] ( #license )
23
31
24
- ## Description
32
+ # Description
25
33
26
34
The package is an intermediate layer between NestJS and RabbitMQ. It provides the possibility of synchronous and asynchronous communication between different microservices.
27
35
28
- ## Motivation
36
+ # Motivation
29
37
30
38
Since the microservice architecture has become very popular, I needed a library that provides the possibility of communicating with services written in different programming languages or frameworks.
31
39
32
40
Using the [ @golevelup/nestjs-rabbitmq ] ( https://www.npmjs.com/package/@golevelup/nestjs-rabbitmq ) package under the hood, which is a great package, but I needed some customizations.
33
41
34
- ## Usage
42
+ # Usage
35
43
36
- ### Installation
44
+ ## Installation
37
45
38
46
``` bash
39
47
$ npm install --save @djereg/nestjs-rabbitmq
40
48
```
41
49
42
- ### Configuration
50
+ ## Configuration
43
51
44
52
The RabbitMQ connection configuration is done through environment variables.
45
53
@@ -53,7 +61,7 @@ RABBITMQ_QUEUE=
53
61
RABBITMQ_EXCHANGE=
54
62
```
55
63
56
- ### Module Initialization
64
+ ## Module Initialization
57
65
58
66
``` typescript
59
67
import {RabbitMQModule } from " @djereg/nestjs-rabbitmq" ;
@@ -67,14 +75,14 @@ export class AppModule {
67
75
}
68
76
```
69
77
70
- ## Events
78
+ # Events
71
79
72
80
Provides an event based asynchronous communication between services.
73
81
74
- Works very similarly to the [ NestJS event system] ( https://docs.nestjs.com/techniques/events ) , as it wraps it. When an event-type message is received, an event is emitted with the help of the built-in event emitter, and the methods subscribed to the
82
+ Works very similarly to the [ NestJS event system] ( https://docs.nestjs.com/techniques/events ) , as it wraps it. When an event-type message is received, an event is emitted with the help of the built-in event emitter, and the methods listening to the
75
83
event perform an action.
76
84
77
- ### Emitting Event
85
+ ## Emitting events
78
86
79
87
``` typescript
80
88
import {EventEmitter } from ' @djereg/nestjs-rabbitmq' ;
@@ -84,6 +92,7 @@ export class UserService {
84
92
constructor (
85
93
private readonly eventEmitter : EventEmitter
86
94
) {
95
+ //
87
96
}
88
97
89
98
public async createUser(user : User ) {
@@ -94,39 +103,43 @@ export class UserService {
94
103
}
95
104
```
96
105
97
- ### Subscribing to Event
106
+ ## Listening to events
98
107
99
108
``` typescript
100
- import {Event } from ' @djereg/nestjs-rabbitmq' ;
109
+ import {OnMessageEvent } from ' @djereg/nestjs-rabbitmq' ;
101
110
102
111
export class NotificationService {
103
112
104
- @Event (' user.created' )
113
+ @OnMessageEvent (' user.created' )
105
114
public async handleUserCreated(user : User ) {
106
115
// Send notification logic
107
116
}
108
117
}
109
118
```
110
119
111
- You can subscribe to multiple events by adding multiple decorators.
120
+ You can listen to multiple events by adding multiple decorators.
112
121
113
122
``` typescript
114
- @Event (' user.created' )
115
- @Event (' user.updated' )
123
+ @OnMessageEvent (' user.created' )
124
+ @OnMessageEvent (' user.updated' )
116
125
async function handler() {
117
-
126
+ // Do something
118
127
}
119
128
```
120
129
121
- ## RPC
130
+ ## Subscribing to events
131
+
132
+ At startup the exchange and queue will be created automatically, and the events listening to will be registered as routing keys.
133
+
134
+ # RPC
122
135
123
136
Provides the possibility of synchronous like asynchronous communication between services.
124
137
125
138
Uses the [ JSON-RPC 2.0] ( https://www.jsonrpc.org/specification ) protocol for communication.
126
139
127
- ### Setup
140
+ ## Setup
128
141
129
- Before using the client, you need set it up in the module.
142
+ Before using the client, you need to define the client in the module.
130
143
131
144
``` typescript
132
145
import {RabbitMQModule } from " @djereg/nestjs-rabbitmq" ;
@@ -142,54 +155,60 @@ import {RabbitMQModule} from "@djereg/nestjs-rabbitmq";
142
155
})
143
156
```
144
157
145
- After initialization, you can use the client in the service.
158
+ After initialization, you can use the client in a service.
146
159
147
- ### Method Call
160
+ ## Calling remote procedures
148
161
149
- A synchronous-like call to the method of service which returns a result .
162
+ Inject the previously defined client into a service and call the remote procedures like the example below .
150
163
151
164
``` typescript
152
165
import {Client , InjectClient } from ' @djereg/nestjs-rabbitmq' ;
166
+ import {Injectable } from " @nestjs/common" ;
153
167
154
- class UserController {
168
+ @Injectable ()
169
+ class UserService {
155
170
156
171
constructor (
157
172
@InjectClient (' users' )
158
173
private readonly users : Client
159
174
) {
175
+ //
160
176
}
161
177
162
178
public async createUser(dto : UserCreateDto ) {
163
179
const user = this .users .call (' create' , dto );
164
-
165
- // Do something with user
180
+ // Do something with the user data returned
166
181
}
167
182
}
168
183
```
169
184
185
+ ## Registering remote procedures
186
+
187
+ Create a service and add the ` @RemoteProcedure ` decorator to the method you want to expose.
188
+
189
+ Adding the decorator without parameters will use the method name as the remote procedure name.
190
+ Specifying the name explicitly will use the specified name.
191
+
170
192
``` typescript
171
- import {Method } from ' @djereg/nestjs-rabbitmq' ;
193
+ import {RemoteProcedure } from ' @djereg/nestjs-rabbitmq' ;
172
194
173
195
class UserService {
174
196
175
- @Method ()
197
+ @RemoteProcedure ()
176
198
create(dto : CreateUserDto ) {
177
- // Create user logic
178
- const user = db .createUser (dto );
179
-
180
- return user ;
199
+ // Create a user and return it
181
200
}
182
201
183
202
// Also you can specify the method name explicitly
184
- @Method (' delete' )
203
+ @RemoteProcedure (' delete' )
185
204
deleteUserMethod(id : number ) {
186
- // Delete user logic
205
+ // Delete the user somehow
187
206
}
188
207
}
189
208
190
209
```
191
210
192
- ### Notification
211
+ ## Notification
193
212
194
213
An asynchronous call to the method of service which does not return a result.
195
214
@@ -202,33 +221,35 @@ class UserController {
202
221
@InjectClient (' notifications' )
203
222
private readonly notifications : Client
204
223
) {
224
+ //
205
225
}
206
226
207
227
public async createUser(dto : UserCreateDto ) {
208
- // Create user logic
209
-
228
+ // Create the user and notify the notification service
210
229
this .notifications .notify (' userCreated' , user );
211
230
}
212
231
}
213
232
```
214
233
215
- ### Batch Call
234
+ ## Batch Call
216
235
217
236
A grouped call to multiple methods of service. Returns a list of results.
218
237
219
238
``` typescript
220
239
import {Client , InjectClient } from ' @djereg/nestjs-rabbitmq' ;
221
- import {basename } from " @angular-devkit/core " ;
240
+ import {Injectable } from " @nestjs/common " ;
222
241
223
- class UserController {
242
+ @Injectable ()
243
+ class Mathervice {
224
244
225
245
constructor (
226
246
@InjectClient (' math' )
227
247
private readonly math : Client
228
248
) {
249
+ //
229
250
}
230
251
231
- public async createUser( dto : UserCreateDto ) {
252
+ public async batchCall( ) {
232
253
233
254
const batch = this .math .batch ();
234
255
@@ -239,16 +260,71 @@ class UserController {
239
260
240
261
// The notification method can also be used in the
241
262
// batch, but it will not return a result
242
- batch .notify (' somethin ' , {a: 1 , b: 2 });
263
+ batch .notify (' something ' , {a: 1 , b: 2 });
243
264
batch .notify (' anything' , {a: 1 , b: 2 });
244
265
245
266
const results = await batch .send ();
246
267
247
- // Do something with user
268
+ // Do something with the results
269
+ }
270
+ }
271
+ ```
272
+
273
+ # Lifecycle events
274
+
275
+ The package emits events during the message processing.
276
+ You can listen to these events and perform some actions.
277
+
278
+ Add the corresponding decorator to the method you want to execute.
279
+
280
+ ## MessagePublishing
281
+
282
+ Emitted before the message is published to the exchange.
283
+
284
+ ``` typescript
285
+ import {OnMessagePublishing , MessagePublishingEvent } from ' @djereg/nestjs-rabbitmq' ;
286
+
287
+ class UserService {
288
+
289
+ @OnMessagePublishing ()
290
+ async handlePublishing(event : MessagePublishingEvent ) {
291
+ // Do something with the event
292
+ }
293
+ }
294
+ ```
295
+
296
+ ## MessageProcessing
297
+
298
+ Emitted before the message is processed.
299
+
300
+ ``` typescript
301
+ import {OnMessageProcessing , MessageProcessingEvent } from ' @djereg/nestjs-rabbitmq' ;
302
+
303
+ class UserService {
304
+
305
+ @OnMessageProcessing ()
306
+ async handleProcessing(event : MessageProcessingEvent ) {
307
+ // Do something with the event
308
+ }
309
+ }
310
+ ```
311
+
312
+ ## MessageProcessed
313
+
314
+ Emitted after the message is processed.
315
+
316
+ ``` typescript
317
+ import {OnMessageProcessed , MessageProcessedEvent } from ' @djereg/nestjs-rabbitmq' ;
318
+
319
+ class UserService {
320
+
321
+ @OnMessageProcessed ()
322
+ async handleProcessed(event : MessageProcessedEvent ) {
323
+ // Do something with the event
248
324
}
249
325
}
250
326
```
251
327
252
- ## License
328
+ # License
253
329
254
330
[ MIT licensed] ( LICENSE )
0 commit comments