Skip to content

Instantly share code, notes, and snippets.

@Mcdavid95
Created February 27, 2020 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mcdavid95/4f5679e8d58aa538dd48bd8cc4b7fbb8 to your computer and use it in GitHub Desktop.
Save Mcdavid95/4f5679e8d58aa538dd48bd8cc4b7fbb8 to your computer and use it in GitHub Desktop.
SES RabbitMQ Tutorial
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