Skip to content

Commit 33a94d4

Browse files
committed
Establish rabbitmq comms
1 parent e99a0b7 commit 33a94d4

File tree

5 files changed

+151
-2
lines changed

5 files changed

+151
-2
lines changed

matching-service/src/services/rabbitMqService.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ export const sendToQueue = async (
103103
try {
104104
if (!channel) throw new Error("RabbitMQ channel is not initialized");
105105

106-
await channel.assertQueue(queue);
106+
await channel.assertQueue(queue, {
107+
durable: true,
108+
expires: 300000, //expire after 5 minutes of idle
109+
});
107110

108111
await channel.sendToQueue(queue, Buffer.from(JSON.stringify(payload)));
109112
console.log(`User sent to RabbitMQ queue "${queue}":`, payload);

peerprep-fe/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@radix-ui/react-select": "^2.1.1",
2121
"@radix-ui/react-slot": "^1.1.0",
2222
"@types/js-cookie": "^3.0.6",
23+
"amqplib": "^0.10.4",
2324
"axios": "^1.7.7",
2425
"class-variance-authority": "^0.7.0",
2526
"clsx": "^2.1.1",

peerprep-fe/pnpm-lock.yaml

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

peerprep-fe/src/components/navbar/Navbar.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import Link from 'next/link';
44
import { logout } from '@/lib/auth';
5+
import { sendMessageToQueue } from '@/lib/rabbitmq';
56
import { Button } from '@/components/ui/button';
7+
import { axiosAuthClient } from '@/network/axiosClient';
68
import { UserCircle, LogOut } from 'lucide-react';
79
import {
810
DropdownMenu,
@@ -23,6 +25,26 @@ export default function Navbar() {
2325
}
2426
};
2527

28+
const getProfileDetails = async () => {
29+
const result = await axiosAuthClient.get('/auth/verify-token');
30+
return result.data.data;
31+
};
32+
33+
const handleMatchClick = async () => {
34+
try {
35+
const profileDetails = await getProfileDetails();
36+
const message = {
37+
_id: profileDetails.id,
38+
name: profileDetails.username,
39+
topic: 'TO BE ADDED',
40+
difficulty: 'TO BE ADDED',
41+
};
42+
await sendMessageToQueue(message);
43+
} catch (err) {
44+
console.error('Error in handleMatchClick:', err);
45+
}
46+
};
47+
2648
return (
2749
<nav className="fixed top-0 z-10 w-full bg-gray-800 p-4">
2850
<div className="mx-auto flex max-w-7xl items-center justify-between">
@@ -40,7 +62,13 @@ export default function Navbar() {
4062
</Link>
4163
{/* Admin users should be able to add questions instead of match */}
4264
{!user?.isAdmin ? (
43-
<Link href="/match" className="text-gray-300 hover:text-white">
65+
// TODO: Change this such that it will pop up a toast for users to select topic and difficulty, the subsequent button will
66+
// then call "handleMatchClick"
67+
<Link
68+
href="/match"
69+
className="text-gray-300 hover:text-white"
70+
onClick={() => handleMatchClick()}
71+
>
4472
Match
4573
</Link>
4674
) : (

peerprep-fe/src/lib/rabbitmq.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use server';
2+
3+
import { connect } from 'amqplib';
4+
5+
export const sendMessageToQueue = async (message: Record<string, any>) => {
6+
try {
7+
// 1. Connect to RabbitMQ server
8+
const connection = await connect(process.env.RABBITMQ_URL);
9+
10+
// 2. Create a channel
11+
const channel = await connection.createChannel();
12+
13+
// 3. Ensure the queue exists
14+
const queue = process.env.MATCHING_SERVICE_QUEUE;
15+
await channel.assertQueue(queue, {
16+
durable: true,
17+
});
18+
19+
// 4. Send a message to the queue
20+
const messageBuffer = Buffer.from(JSON.stringify(message));
21+
channel.sendToQueue(queue, messageBuffer, {
22+
persistent: true,
23+
});
24+
25+
console.log(`Message sent to queue "${queue}":`, message);
26+
27+
// 5. Close the channel and connection
28+
setTimeout(() => {
29+
channel.close();
30+
connection.close();
31+
}, 500);
32+
} catch (err) {
33+
console.error('Error sending message to RabbitMQ:', err);
34+
throw err;
35+
}
36+
};

0 commit comments

Comments
 (0)