Node.js Paris - 09/10/19
@Frichti X RabbitMQ
Aymeric Dijoux
Kajan Siva
Message broker open-source avec pleins d'avantages :
- Asynchronicité
- Découplage
- Permet une meilleure scalabilité
Utilise le protocole AMQP.
Quatres concepts-clés :
- Messages
- Exchanges
- Bindings
- Queues
Information transmis du producer vers le consumer, à travers RabbitMQ.
Est constitué d'un contenu et d'une clé de routage.
Reçoit les messages du producer et les transmets dans les queues selon les règles définis par le type de l'exchange.
Une binding est le lien entre une queue et un exchange.
Zone tampon qui stocke les messages attendant d'être consommés.
Ack: accepte le message.
Unack : replace le message en tête de queue.
Reject : rejette le message.
Fonctionnalité permettant de capturer les messages qui ne sont pas livrables.
Quatres concepts-clés :
- Messages
- Exchanges
- Bindings
- Queues
"exchanges": [
{
"name": "delivery",
"type": "topic",
},
{
"name": "delivery-dead-letter",
"type": "topic",
}
]
"queues": [
{
"name": "delivery-notifications",
"deadLetter": "delivery-dead-letter",
}
];
"bindings": [
{
"exchange": "delivery",
"target": "delivery-notifications",
"keys": [
"event.task.created",
"event.task.updated",
"event.eta.updated",
"event.tour.updated"
]
}
]
rabbit.handle(
{
type: 'event.task.created',
queue: 'delivery-notifications',
},
async (message) => {
try {
await notificationCreated(message);
message.ack();
} catch (err) {
message.nack();
throw err;
}
},
);
{
"notificationCreated": {
"title": "Suivi de commande",
"messageToRender": "{{{firstName}}} {{{lastName}}} vient de passer une commande {{{name}}} et c'est vous qui allez la recevoir.\nElle devrait arriver {{{renderDeliveryMessage}}}."
}
}
const messageToSend = mustache.render(messageToRender, objectToRender);
async function start() {
rabbit.handle('send', sendHandler.send(rabbit));
rabbit.startSubscription('notification-send');
rabbit.startSubscription('notification-push-os');
rabbit.startSubscription('notification-email');
}
rabbit.configure(settings).done(() => {
start()
.catch((err) => {
process.exit(1);
});
});
if (body.phone) {
transports = 'sms';
}
try {
await senders[transports](body);
} catch (err) {
throw err;
}
async function sendDeliveryNotif(message = {}) {
const res = await send(message);
if (res.status === 200 && res.body.errors) {
throw new Error(`cant push notification from OneSignal ${JSON.stringify(res.body.errors)} `);
}
return res.body;
}
"queues":[{
"name": "notification-push-os",
"deadLetter": "notification",
}]
"exchanges": [
{
"name": "notification",
"type": "topic",
},
{
"name": "notification-push",
"type": "topic",
}
]
"binding":[
{
"exchange": "notification-push",
"target": "notification-push-os",
"keys": ["#"]
}
]
if (Object.getOwnPropertyNames(message.properties.headers).length
&& queue !== 'notification-push-os'
&& queue !== 'notification-email') {
body.transports = 'sms';
}
const { transports } = body;
try {
await senders[transports](body);
} catch (err) {
throw err;
}
"queues":[{
"name": "notification-email",
"deadLetter": "notification",
},
{
"name": "notification-email-delayed",
"deadLetter": "notification-email",
}]
"exchanges": [
{
"name": "notification-email-delayed",
"type": "topic",
},
{
"name": "notification-email",
"type": "topic",
}
]
"binding":[
{
"exchange": "notification-email",
"target": "notification-email",
"keys": [
"#"
]
},
{
"exchange": "notification-email-delayed",
"target": "notification-email-delayed",
"keys": [
"#"
]
}
]
const transport = source === 'website-desktop' ? 'email' : 'push';
const notification = await notificationService.send(
...message
90 * MINUTE, // Important part
);
const { transports } = ctx.request.body;
const exchange = config.transportAllowed[transports] || 'notification';
if (ctx.request.headers['x-delayed']) {
await rabbit.publish(`${exchange}-delayed`, {
type: 'send',
body: {},
expiresAfter: ctx.request.headers['x-delayed'] || undefined,
});
}
async function sendMail(message) {
try {
return sendPost(message);
} catch (err) {
throw new Error(
`cant push notification from OneSignal ${JSON.stringify(err)} `
);
}
}
+
=
❤️