Skip to content

Commit 134bb73

Browse files
committed
feat: add usecase Pub-sub
1 parent 644b37a commit 134bb73

File tree

11 files changed

+552
-34
lines changed

11 files changed

+552
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
**/node_modules
2+
**/dist

Nest-microservices/.gitignore

Lines changed: 0 additions & 34 deletions
This file was deleted.

Pub-sub/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Subscriber, Publisher
2+
1. Establish connection to the RabbitMQ
3+
2. Create a channel with RabbitMQ
4+
3. Create or connect to exchange - await `channel.assertExchange`('name_exchange', 'type_exchange', options)
5+
6+
## Subscriber
7+
4. Create a queue - await `channel.assertQueue`('queue_id', options)
8+
5. Bind this queue to exchange (await `channel.bindQueue`('queue_id', 'exchange_name', 'event_name'))
9+
6. Start to consume messages from queue - `channel.consume`('queue_id', cb)
10+
7. If we get message and this message requires of acknowledgement - send message to this replyQueue:
11+
- `channel.sendToQueue`('reply_to_queue', some_data, {correlationId: message.fields.correlationId})
12+
13+
## Publisher
14+
4. Create a replyQueue - await `channel.assertQueue`('', { exclusive: true })
15+
5. Start to consume messages from replyQueue - `channel.consume`('queue_id', cb)
16+
6. Send message to exchange:
17+
- `channel.publish`('name_exchange', 'event_name', message, {replyTo: 'reply_queue_id', correlationId: string })
18+
19+
20+
## How does it work?
21+
- [Publisher]: Send a message to the exchange
22+
- [Exchange]: Gets this message and redirects to the subscriber
23+
- [Subscriber]: Check if there is replyTo option and it that is case - reply to this queue
24+
- [Publisher]: Gets message from the replyQueue
25+
26+
27+
![alt text](./schema.png)

Pub-sub/docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: "3"
2+
services:
3+
rabbitmq:
4+
image: rabbitmq:3-management
5+
restart: always
6+
ports:
7+
- '15672:15672'
8+
- '5672:5672'

Pub-sub/package-lock.json

Lines changed: 272 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Pub-sub/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "rmq-demo",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"amqplib": "^0.9.0"
13+
},
14+
"devDependencies": {
15+
"@types/amqplib": "^0.8.2",
16+
"@types/node": "^17.0.32"
17+
}
18+
}

0 commit comments

Comments
 (0)