Created
February 27, 2020 18:42
-
-
Save Mcdavid95/4f5679e8d58aa538dd48bd8cc4b7fbb8 to your computer and use it in GitHub Desktop.
SES RabbitMQ Tutorial
This file contains 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
const dotenv = require('dotenv'); | |
const EmailService = require('./email.service'); | |
dotenv.config(); | |
const queue = 'email-task'; | |
const open = require('amqplib').connect(process.env.AMQP_SERVER); | |
// Publisher | |
const publishMessage = payload => open.then(connection => connection.createChannel()) | |
.then(channel => channel.assertQueue(queue) | |
.then(() => channel.sendToQueue(queue, Buffer.from(JSON.stringify(payload))))) | |
.catch(error => console.warn(error)); | |
// Consumer | |
const consumeMessage = () => { | |
open.then(connection => connection.createChannel()).then(channel => channel.assertQueue(queue).then(() => { | |
console.log(' [*] Waiting for messages in %s. To exit press CTRL+C', queue); | |
return channel.consume(queue, (msg) => { | |
if (msg !== null) { | |
const { mail, subject, template } = JSON.parse(msg.content.toString()); | |
console.log(' [x] Received %s', mail); | |
// send email via aws ses | |
EmailService.sendMail(mail, subject, template).then(() => { | |
channel.ack(msg); | |
}); | |
} | |
}); | |
})).catch(error => console.warn(error)); | |
}; | |
module.exports = { | |
publishMessage, | |
consumeMessage | |
} | |
require('make-runnable'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment