Skip to content

Commit eaf0e5f

Browse files
authored
Merge pull request #10 from djereg/dev
docs: update readme
2 parents 81b8f84 + 3cfc1d7 commit eaf0e5f

File tree

1 file changed

+126
-50
lines changed

1 file changed

+126
-50
lines changed

README.md

Lines changed: 126 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# First of all
1+
# NestJS RabbitMQ
22

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.**
45

5-
# NestJS RabbitMQ
6+
The package is part of the [rabbitmq-multiverse](https://github.com/djereg/rabbitmq-multiverse).
67

7-
## Table of Contents
8+
# Table of Contents
89

910
- [Description](#description)
1011
- [Motivation](#motivation)
@@ -13,33 +14,40 @@
1314
- [Configuration](#configuration)
1415
- [Module Initialization](#module-initialization)
1516
- [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)
1820
- [RPC](#rpc)
1921
- [Setup](#setup)
20-
- [Method Call](#method-call)
22+
- [Calling remote procedures](#calling-remote-procedures)
23+
- [Registering remote procedures](#registering-remote-procedures)
2124
- [Notification](#notification)
2225
- [Batch Call](#batch-call)
26+
- [Lifecycle events](#lifecycle-events)
27+
- [MessagePublishing](#messagepublishing)
28+
- [MessageProcessing](#messageprocessing)
29+
- [MessageProcessed](#messageprocessed)
30+
- [License](#license)
2331

24-
## Description
32+
# Description
2533

2634
The package is an intermediate layer between NestJS and RabbitMQ. It provides the possibility of synchronous and asynchronous communication between different microservices.
2735

28-
## Motivation
36+
# Motivation
2937

3038
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.
3139

3240
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.
3341

34-
## Usage
42+
# Usage
3543

36-
### Installation
44+
## Installation
3745

3846
```bash
3947
$ npm install --save @djereg/nestjs-rabbitmq
4048
```
4149

42-
### Configuration
50+
## Configuration
4351

4452
The RabbitMQ connection configuration is done through environment variables.
4553

@@ -53,7 +61,7 @@ RABBITMQ_QUEUE=
5361
RABBITMQ_EXCHANGE=
5462
```
5563

56-
### Module Initialization
64+
## Module Initialization
5765

5866
```typescript
5967
import {RabbitMQModule} from "@djereg/nestjs-rabbitmq";
@@ -67,14 +75,14 @@ export class AppModule {
6775
}
6876
```
6977

70-
## Events
78+
# Events
7179

7280
Provides an event based asynchronous communication between services.
7381

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
7583
event perform an action.
7684

77-
### Emitting Event
85+
## Emitting events
7886

7987
```typescript
8088
import {EventEmitter} from '@djereg/nestjs-rabbitmq';
@@ -84,6 +92,7 @@ export class UserService {
8492
constructor(
8593
private readonly eventEmitter: EventEmitter
8694
) {
95+
//
8796
}
8897

8998
public async createUser(user: User) {
@@ -94,39 +103,43 @@ export class UserService {
94103
}
95104
```
96105

97-
### Subscribing to Event
106+
## Listening to events
98107

99108
```typescript
100-
import {Event} from '@djereg/nestjs-rabbitmq';
109+
import {OnMessageEvent} from '@djereg/nestjs-rabbitmq';
101110

102111
export class NotificationService {
103112

104-
@Event('user.created')
113+
@OnMessageEvent('user.created')
105114
public async handleUserCreated(user: User) {
106115
// Send notification logic
107116
}
108117
}
109118
```
110119

111-
You can subscribe to multiple events by adding multiple decorators.
120+
You can listen to multiple events by adding multiple decorators.
112121

113122
```typescript
114-
@Event('user.created')
115-
@Event('user.updated')
123+
@OnMessageEvent('user.created')
124+
@OnMessageEvent('user.updated')
116125
async function handler() {
117-
126+
// Do something
118127
}
119128
```
120129

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
122135

123136
Provides the possibility of synchronous like asynchronous communication between services.
124137

125138
Uses the [JSON-RPC 2.0](https://www.jsonrpc.org/specification) protocol for communication.
126139

127-
### Setup
140+
## Setup
128141

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.
130143

131144
```typescript
132145
import {RabbitMQModule} from "@djereg/nestjs-rabbitmq";
@@ -142,54 +155,60 @@ import {RabbitMQModule} from "@djereg/nestjs-rabbitmq";
142155
})
143156
```
144157

145-
After initialization, you can use the client in the service.
158+
After initialization, you can use the client in a service.
146159

147-
### Method Call
160+
## Calling remote procedures
148161

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.
150163

151164
```typescript
152165
import {Client, InjectClient} from '@djereg/nestjs-rabbitmq';
166+
import {Injectable} from "@nestjs/common";
153167

154-
class UserController {
168+
@Injectable()
169+
class UserService {
155170

156171
constructor(
157172
@InjectClient('users')
158173
private readonly users: Client
159174
) {
175+
//
160176
}
161177

162178
public async createUser(dto: UserCreateDto) {
163179
const user = this.users.call('create', dto);
164-
165-
// Do something with user
180+
// Do something with the user data returned
166181
}
167182
}
168183
```
169184

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+
170192
```typescript
171-
import {Method} from '@djereg/nestjs-rabbitmq';
193+
import {RemoteProcedure} from '@djereg/nestjs-rabbitmq';
172194

173195
class UserService {
174196

175-
@Method()
197+
@RemoteProcedure()
176198
create(dto: CreateUserDto) {
177-
// Create user logic
178-
const user = db.createUser(dto);
179-
180-
return user;
199+
// Create a user and return it
181200
}
182201

183202
// Also you can specify the method name explicitly
184-
@Method('delete')
203+
@RemoteProcedure('delete')
185204
deleteUserMethod(id: number) {
186-
// Delete user logic
205+
// Delete the user somehow
187206
}
188207
}
189208

190209
```
191210

192-
### Notification
211+
## Notification
193212

194213
An asynchronous call to the method of service which does not return a result.
195214

@@ -202,33 +221,35 @@ class UserController {
202221
@InjectClient('notifications')
203222
private readonly notifications: Client
204223
) {
224+
//
205225
}
206226

207227
public async createUser(dto: UserCreateDto) {
208-
// Create user logic
209-
228+
// Create the user and notify the notification service
210229
this.notifications.notify('userCreated', user);
211230
}
212231
}
213232
```
214233

215-
### Batch Call
234+
## Batch Call
216235

217236
A grouped call to multiple methods of service. Returns a list of results.
218237

219238
```typescript
220239
import {Client, InjectClient} from '@djereg/nestjs-rabbitmq';
221-
import {basename} from "@angular-devkit/core";
240+
import {Injectable} from "@nestjs/common";
222241

223-
class UserController {
242+
@Injectable()
243+
class Mathervice {
224244

225245
constructor(
226246
@InjectClient('math')
227247
private readonly math: Client
228248
) {
249+
//
229250
}
230251

231-
public async createUser(dto: UserCreateDto) {
252+
public async batchCall() {
232253

233254
const batch = this.math.batch();
234255

@@ -239,16 +260,71 @@ class UserController {
239260

240261
// The notification method can also be used in the
241262
// batch, but it will not return a result
242-
batch.notify('somethin', {a: 1, b: 2});
263+
batch.notify('something', {a: 1, b: 2});
243264
batch.notify('anything', {a: 1, b: 2});
244265

245266
const results = await batch.send();
246267

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
248324
}
249325
}
250326
```
251327

252-
## License
328+
# License
253329

254330
[MIT licensed](LICENSE)

0 commit comments

Comments
 (0)