|
1 | 1 | import amqp, { Channel } from "amqplib";
|
2 |
| -import { storeUser } from "../model/userModel"; |
3 |
| -import { User } from "../types"; |
4 |
| -import { processNewUser } from "./matchingService"; |
5 | 2 |
|
6 | 3 | let channel: Channel;
|
7 | 4 | const rabbit_url = process.env.RABBITMQ_URL || "amqp://localhost";
|
8 | 5 |
|
9 |
| -export const initRabbitMQ = async (queue: string): Promise<void> => { |
| 6 | +/** |
| 7 | + * Establish a connection to RabbitMQ |
| 8 | + */ |
| 9 | +export const initRabbitMQ = async (): Promise<void> => { |
10 | 10 | try {
|
11 | 11 | const connection = await amqp.connect(rabbit_url);
|
12 | 12 | channel = await connection.createChannel();
|
13 |
| - await channel.assertQueue(queue, { durable: true }); |
14 |
| - console.log(`RabbitMQ initialised and subscribed to ${queue}`); |
15 |
| - |
16 |
| - // Start consuming messages from matching-service |
17 |
| - channel.consume(queue, async (msg) => { |
18 |
| - if (msg !== null) { |
19 |
| - const user: User = JSON.parse(msg.content.toString()); |
20 |
| - console.log("Received message from queue:", user); |
21 |
| - |
22 |
| - // Store the user into Redis |
23 |
| - try { |
24 |
| - await processNewUser(user); |
25 |
| - } catch (err) { |
26 |
| - console.error("Error storing user in Redis:", err); |
27 |
| - } |
28 |
| - |
29 |
| - channel.ack(msg); |
30 |
| - } |
31 |
| - }); |
| 13 | + console.log("Connected to RabbitMQ"); |
32 | 14 | } catch (err) {
|
33 | 15 | console.error("RabbitMQ connection error:", err);
|
34 | 16 | }
|
35 | 17 | };
|
36 | 18 |
|
| 19 | +/** |
| 20 | + * Get the RabbitMQ channel |
| 21 | + * @returns RabbitMQ channel |
| 22 | + */ |
37 | 23 | export const getChannel = (): amqp.Channel => {
|
38 | 24 | if (!channel) {
|
39 | 25 | throw new Error("RabbitMQ channel is not initialized");
|
40 | 26 | }
|
41 | 27 | return channel;
|
42 | 28 | };
|
43 | 29 |
|
44 |
| -// Send a payload to a specified RabbitMQ queue |
| 30 | +/** |
| 31 | + * Subscribes to a RabbitMQ queue and calls the callback function when a message is received. |
| 32 | + * @param queue queue to subscribe to |
| 33 | + * @param callback callback when message is received |
| 34 | + */ |
| 35 | +export const subscribeToQueue = async <T>( |
| 36 | + queue: string, |
| 37 | + callback: (message: T) => void, |
| 38 | +): Promise<void> => { |
| 39 | + try { |
| 40 | + if (!channel) throw new Error("RabbitMQ channel is not initialized"); |
| 41 | + |
| 42 | + await channel.assertQueue(queue); |
| 43 | + console.log(`Subscribed to RabbitMQ queue "${queue}"`); |
| 44 | + |
| 45 | + channel.consume(queue, (msg) => { |
| 46 | + if (msg !== null) { |
| 47 | + const message: T = JSON.parse(msg.content.toString()); |
| 48 | + console.log( |
| 49 | + `Received message from RabbitMQ queue "${queue}":`, |
| 50 | + message, |
| 51 | + ); |
| 52 | + callback(message); |
| 53 | + channel.ack(msg); |
| 54 | + } |
| 55 | + }); |
| 56 | + } catch (err) { |
| 57 | + console.error(`Error subscribing to RabbitMQ queue "${queue}":`, err); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * Sends a payload to a RabbitMQ queue |
| 63 | + * @param queue queue to send payload to |
| 64 | + * @param payload payload object to send |
| 65 | + */ |
45 | 66 | export const sendToQueue = async (
|
46 | 67 | queue: string,
|
47 | 68 | payload: Object,
|
|
0 commit comments