Created
March 22, 2022 15:03
-
-
Save agusrichard/3d8f57923245c74201ad332d4ba46445 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import amqp, { Message } from 'amqplib/callback_api' | |
const createMQConsumer = (amqpURl: string, queueName: string) => { | |
console.log('Connecting to RabbitMQ...') | |
return () => { | |
amqp.connect(amqpURl, (errConn, conn) => { | |
if (errConn) { | |
throw errConn | |
} | |
conn.createChannel((errChan, chan) => { | |
if (errChan) { | |
throw errChan | |
} | |
console.log('Connected to RabbitMQ') | |
chan.assertQueue(queueName, { durable: true }) | |
chan.consume(queueName, (msg: Message | null) => { | |
if (msg) { | |
const parsed = JSON.parse(msg.content.toString()) | |
switch (parsed.action) { | |
case 'REGISTER': | |
console.log('Consuming REGISTER action', parsed.data) | |
break | |
case 'LOGIN': | |
console.log('Consuming LOGIN action', parsed.data) | |
break | |
default: | |
break | |
} | |
} | |
}, { noAck: true }) | |
}) | |
}) | |
} | |
} | |
export default createMQConsumer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment