Skip to content

Commit 2278acb

Browse files
authored
Merge pull request #59 from feliciagan/matching-mq
set up mq
2 parents 7a7d04d + 667f44c commit 2278acb

File tree

10 files changed

+350
-11
lines changed

10 files changed

+350
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
coverage
2+
node_modules
3+
tests
4+
.env*
5+
*.md

backend/matching-service/.env.sample

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@ NODE_ENV=development
22
PORT=3002
33

44
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
5+
6+
7+
## FOR RABBITMQ, comment out the variables under whichever use case (1) or (2) that is not applicable
8+
# (1) RabbitMq for running matching service individually
9+
RABBITMQ_ADDR=amqp://localhost:5672 #comment out if use case is (2)
10+
11+
# (2) RabbitMq for running matching service with other services using docker compose
12+
RABBITMQ_DEFAULT_USER=admin #comment out if use case is (1)
13+
RABBITMQ_DEFAULT_PASS=password #comment out if use case is (1)
14+
RABBITMQ_ADDR=amqp://admin:password@rabbitmq:5672 #comment out if use case is (1)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:20-alpine
2+
3+
WORKDIR /matching-service
4+
5+
COPY package*.json ./
6+
7+
RUN npm ci
8+
9+
COPY . .
10+
11+
EXPOSE 3002
12+
13+
CMD ["npm", "run", "dev"]

backend/matching-service/README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
## Setting-up Matching Service
44

5-
1. In the `matching-service` directory, create a copy of the `.env.sample` file and name it `.env`.
5+
1. In the `matching-service` directory, create a copy of the `.env.sample` file and name it `.env`. If you are looking to run matching service with the other services using docker-compose, comment out the variable `RABBITMQ_ADDR` under use case (1) in the .env file. Otherwise, if you are looking to run matching service individually, comment out the variables `RABBITMQ_DEFAULT_USER`, `RABBITMQ_DEFAULT_PASS` and `RABBITMQ_ADDR` under use case (2) in the .env file.
66

7-
## Running Matching Service without Docker
7+
2. If you are running matching service together with other services using docker-compose, to set up credentials for RabbitMq, update the RabbitMq variables in the `.env` file. Update `RABBITMQ_DEFAULT_USER` and `RABBITMQ_DEFAULT_PASS` to what you want, then update `RABBITMQ_ADDR` to be `amqp://<RABBITMQ_DEFAULT_USER>:<RABBITMQ_DEFAULT_PASS>@rabbitmq:5672`.
8+
You can access RabbitMq management user interface locally with the username in `RABBITMQ_DEFAULT_USER` and password in `RABBITMQ_DEFAULT_PASS` at http://localhost:15672.
89

9-
1. Follow the instructions [here](https://nodejs.org/en/download/package-manager) to set up Node v20.
10+
3. If you are running matching service individually, you do not need to make any changes to `RABBITMQ_ADDR`. You can access RabbitMq management user interface locally with the username `guest` and password `guest` at http://localhost:15672.
1011

11-
2. Open Command Line/Terminal and navigate into the `matching-service` directory.
12+
## Running Matching Service Individually with Docker
1213

13-
3. Run the command: `npm install`. This will install all the necessary dependencies.
14+
1. Set up and run RabbitMq locally on your computer with the command `docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:4.0-management`.
1415

15-
4. Run the command `npm start` to start the Matching Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes.
16+
2. Follow the instructions [here](https://nodejs.org/en/download/package-manager) to set up Node v20.
17+
18+
3. Open Command Line/Terminal and navigate into the `matching-service` directory.
19+
20+
4. Run the command: `npm install`. This will install all the necessary dependencies.
21+
22+
5. Run the command `npm start` to start the Matching Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes.
1623

1724
## After running
1825

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import amqplib, { Connection } from "amqplib";
2+
import dotenv from "dotenv";
3+
import { matchUsers } from "../utils/mq_utils";
4+
5+
dotenv.config();
6+
7+
let mrConnection: Connection;
8+
const queue = "match_requests";
9+
10+
export const connectRabbitMq = async () => {
11+
try {
12+
mrConnection = await amqplib.connect(`${process.env.RABBITMQ_ADDR}`);
13+
const consumerChannel = await mrConnection.createChannel();
14+
await consumerChannel.assertQueue(queue);
15+
16+
consumerChannel.consume(queue, async (msg) => {
17+
if (msg !== null) {
18+
try {
19+
await matchUsers(msg.content.toString());
20+
} catch (error) {
21+
console.error(error);
22+
}
23+
consumerChannel.ack(msg);
24+
}
25+
});
26+
} catch (error) {
27+
console.error(error);
28+
process.exit(1);
29+
}
30+
};
31+
32+
type MatchRequestMessage = {
33+
userId: string;
34+
categories: string[] | string;
35+
complexities: string[] | string;
36+
sentTimestamp: number;
37+
ttlInSecs: number;
38+
};
39+
40+
export const sendRabbitMq = async (data: MatchRequestMessage) => {
41+
try {
42+
const senderChannel = await mrConnection.createChannel();
43+
senderChannel.sendToQueue(queue, Buffer.from(JSON.stringify(data)));
44+
} catch (error) {
45+
console.log(error);
46+
throw new Error("Failed to send match request");
47+
}
48+
};

backend/matching-service/package-lock.json

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

backend/matching-service/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"author": "",
1414
"license": "ISC",
1515
"dependencies": {
16+
"amqplib": "^0.10.4",
1617
"cors": "^2.8.5",
1718
"dotenv": "^16.4.5",
1819
"express": "^4.21.1",
@@ -21,6 +22,7 @@
2122
},
2223
"devDependencies": {
2324
"@eslint/js": "^9.12.0",
25+
"@types/amqplib": "^0.10.5",
2426
"@types/cors": "^2.8.17",
2527
"@types/express": "^5.0.0",
2628
"@types/jest": "^29.5.13",

backend/matching-service/server.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
import app from "./app.ts";
2+
import { connectRabbitMq } from "./config/rabbitmq.ts";
23

34
const PORT = process.env.PORT || 3002;
45

56
if (process.env.NODE_ENV !== "test") {
6-
app.listen(PORT, () => {
7-
console.log(
8-
`Matching service server listening on http://localhost:${PORT}`
9-
);
10-
});
7+
connectRabbitMq()
8+
.then(() => {
9+
console.log("RabbitMq connected!");
10+
11+
app.listen(PORT, () => {
12+
console.log(
13+
`Matching service server listening on http://localhost:${PORT}`
14+
);
15+
});
16+
17+
//can use this to test if rabbitmq works for you (import sendRabbitMq from rabbitmq.ts first)
18+
/*const message1 = {
19+
userId: "1",
20+
categories: "Algorithms",
21+
complexities: "Easy",
22+
sentTimestamp: Date.now(),
23+
ttlInSecs: 30,
24+
};
25+
sendRabbitMq(message1);
26+
const message2 = {
27+
userId: "2",
28+
categories: "Algorithms",
29+
complexities: "Medium",
30+
sentTimestamp: Date.now(),
31+
ttlInSecs: 30,
32+
};
33+
sendRabbitMq(message2);*/
34+
})
35+
.catch((err) => {
36+
console.error("Failed to connect to RabbitMq");
37+
console.error(err);
38+
});
1139
}

0 commit comments

Comments
 (0)