Skip to content

Commit

Permalink
Merge pull request #73 from CS3219-AY2425S1/feat/fe_to_rabbitmq
Browse files Browse the repository at this point in the history
Establish rabbitmq comms
  • Loading branch information
ZD292 authored Oct 20, 2024
2 parents e99a0b7 + 33a94d4 commit c8c689e
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 2 deletions.
5 changes: 4 additions & 1 deletion matching-service/src/services/rabbitMqService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ export const sendToQueue = async (
try {
if (!channel) throw new Error("RabbitMQ channel is not initialized");

await channel.assertQueue(queue);
await channel.assertQueue(queue, {
durable: true,
expires: 300000, //expire after 5 minutes of idle
});

await channel.sendToQueue(queue, Buffer.from(JSON.stringify(payload)));
console.log(`User sent to RabbitMQ queue "${queue}":`, payload);
Expand Down
1 change: 1 addition & 0 deletions peerprep-fe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-slot": "^1.1.0",
"@types/js-cookie": "^3.0.6",
"amqplib": "^0.10.4",
"axios": "^1.7.7",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
Expand Down
81 changes: 81 additions & 0 deletions peerprep-fe/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion peerprep-fe/src/components/navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import Link from 'next/link';
import { logout } from '@/lib/auth';
import { sendMessageToQueue } from '@/lib/rabbitmq';
import { Button } from '@/components/ui/button';
import { axiosAuthClient } from '@/network/axiosClient';
import { UserCircle, LogOut } from 'lucide-react';
import {
DropdownMenu,
Expand All @@ -23,6 +25,26 @@ export default function Navbar() {
}
};

const getProfileDetails = async () => {
const result = await axiosAuthClient.get('/auth/verify-token');
return result.data.data;
};

const handleMatchClick = async () => {
try {
const profileDetails = await getProfileDetails();
const message = {
_id: profileDetails.id,
name: profileDetails.username,
topic: 'TO BE ADDED',
difficulty: 'TO BE ADDED',
};
await sendMessageToQueue(message);
} catch (err) {
console.error('Error in handleMatchClick:', err);
}
};

return (
<nav className="fixed top-0 z-10 w-full bg-gray-800 p-4">
<div className="mx-auto flex max-w-7xl items-center justify-between">
Expand All @@ -40,7 +62,13 @@ export default function Navbar() {
</Link>
{/* Admin users should be able to add questions instead of match */}
{!user?.isAdmin ? (
<Link href="/match" className="text-gray-300 hover:text-white">
// TODO: Change this such that it will pop up a toast for users to select topic and difficulty, the subsequent button will
// then call "handleMatchClick"
<Link
href="/match"
className="text-gray-300 hover:text-white"
onClick={() => handleMatchClick()}
>
Match
</Link>
) : (
Expand Down
36 changes: 36 additions & 0 deletions peerprep-fe/src/lib/rabbitmq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use server';

import { connect } from 'amqplib';

export const sendMessageToQueue = async (message: Record<string, any>) => {
try {
// 1. Connect to RabbitMQ server
const connection = await connect(process.env.RABBITMQ_URL);

// 2. Create a channel
const channel = await connection.createChannel();

// 3. Ensure the queue exists
const queue = process.env.MATCHING_SERVICE_QUEUE;
await channel.assertQueue(queue, {
durable: true,
});

// 4. Send a message to the queue
const messageBuffer = Buffer.from(JSON.stringify(message));
channel.sendToQueue(queue, messageBuffer, {
persistent: true,
});

console.log(`Message sent to queue "${queue}":`, message);

// 5. Close the channel and connection
setTimeout(() => {
channel.close();
connection.close();
}, 500);
} catch (err) {
console.error('Error sending message to RabbitMQ:', err);
throw err;
}
};

0 comments on commit c8c689e

Please sign in to comment.