1
1
const amqp = require ( 'amqplib' ) ;
2
+ const { queueNames } = require ( './setup.js' ) ;
3
+ const { matchUsers } = require ( '../services/matchingService.js' ) ;
2
4
3
5
// TODO: Subscribe and acknowledge messages with user info when timeout/user matched
4
6
5
- //const { matchUsers } = require('../services/matchingService');
7
+ // To remember what goes in a subscriber use some Acronym
8
+ // Connect, Assert, Process, E - for Acknowledge
6
9
7
- /*
8
10
async function consumeQueue ( ) {
9
11
try {
12
+ // Connect
10
13
const connection = await amqp . connect ( process . env . RABBITMQ_URL ) ;
11
14
const channel = await connection . createChannel ( ) ;
12
- const exchange = 'matching_exchange';
13
15
14
- // Consuming messages from multiple queues (already created in setup)
15
- const queueNames = ['easy.python', 'easy.java', 'medium.python', 'medium.java', 'hard.python', 'hard.java'];
16
+ // Queues already created in setup.js
16
17
17
18
console . log ( "Waiting for users..." )
18
19
20
+ // Process + subscribe to each queue
19
21
for ( let queueName of queueNames ) {
20
- channel.consume(queueName, (msg) => {
22
+ await channel . consume ( queueName , ( msg ) => {
21
23
if ( msg !== null ) {
22
24
const userData = JSON . parse ( msg . content . toString ( ) ) ;
23
- // const { userId, language, difficulty } = userData;
25
+ const { userId, language, difficulty } = userData ;
24
26
25
27
// Perform the matching logic
26
- // matchUsers( userId, language, difficulty);
27
- console.log(userData );
28
+ console . log ( `Received user ${ userId } with ${ language } and ${ difficulty } ` ) ;
29
+ matchUsers ( userId , language , difficulty ) ;
28
30
31
+ // E- Acknowledge
29
32
channel . ack ( msg ) ;
30
33
}
31
34
} ) ;
@@ -35,4 +38,4 @@ async function consumeQueue() {
35
38
}
36
39
}
37
40
38
- */
41
+ module . exports = { consumeQueue } ;
0 commit comments