├── README.md ├── package.json ├── hello_world ├── recieve.js └── send.js ├── work_queues ├── send_task.js └── consume_task.js ├── Pub_Sub ├── emit_log.js └── receive_logs.js ├── headers ├── emit_log_header.js ├── receive_logs_header_all.js └── receive_logs_header_any.js ├── topics ├── emit_log_topic.js └── receive_logs_topic.js ├── routing ├── emit_log_direct.js └── receive_logs_direct.js └── rpc ├── rpc_server.js └── rpc_client.js /README.md: -------------------------------------------------------------------------------- 1 | # RabbitMQ-NodeJS 2 | 3 | ### RabbitMQ playlist: 4 | https://www.youtube.com/playlist?list=PLrwNNiB6YOA3Z3JfOUMKE6PmnpmVAJgTK 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "RabbitMQ", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "receive.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "amqplib": "^0.6.0", 14 | "uuid": "^8.3.2" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /hello_world/recieve.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const queueName = "wdj"; 4 | 5 | const recieveMsg = async () => { 6 | const connection = await amqplib.connect('amqp://localhost'); 7 | const channel = await connection.createChannel(); 8 | await channel.assertQueue(queueName, {durable: false}); 9 | console.log(`Waiting for messages in queue: ${queueName}`); 10 | channel.consume(queueName, msg => { 11 | console.log("[X] Received:", msg.content.toString()); 12 | }, {noAck: true}) 13 | } 14 | 15 | recieveMsg(); -------------------------------------------------------------------------------- /hello_world/send.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const queueName = "wdj"; 4 | const msg = "comment"; 5 | 6 | const sendMsg = async () => { 7 | const connection = await amqplib.connect('amqp://localhost'); 8 | const channel = await connection.createChannel(); 9 | await channel.assertQueue(queueName, {durable: false}); 10 | channel.sendToQueue(queueName, Buffer.from(msg)); 11 | console.log('Sent: ', msg); 12 | setTimeout(() => { 13 | connection.close(); 14 | process.exit(0); 15 | }, 500) 16 | } 17 | 18 | sendMsg(); -------------------------------------------------------------------------------- /work_queues/send_task.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const queueName = "task"; 4 | const msg = process.argv.slice(2).join(' ') || "Hello World!"; 5 | 6 | const sendTask = async () => { 7 | const connection = await amqplib.connect('amqp://localhost'); 8 | const channel = await connection.createChannel(); 9 | await channel.assertQueue(queueName, {durable: true}); 10 | channel.sendToQueue(queueName, Buffer.from(msg), {persistent: true}); 11 | console.log('Sent: ', msg); 12 | setTimeout(() => { 13 | connection.close(); 14 | process.exit(0); 15 | }, 500) 16 | } 17 | 18 | sendTask(); -------------------------------------------------------------------------------- /Pub_Sub/emit_log.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const exchangeName = "logs"; 4 | const msg = process.argv.slice(2).join(' ') || 'Subscribe, Like, & Comment'; 5 | 6 | const sendMsg = async () => { 7 | const connection = await amqplib.connect('amqp://localhost'); 8 | const channel = await connection.createChannel(); 9 | await channel.assertExchange(exchangeName, 'fanout', {durable: false}); 10 | channel.publish(exchangeName, '', Buffer.from(msg)); 11 | console.log('Sent: ', msg); 12 | setTimeout(() => { 13 | connection.close(); 14 | process.exit(0); 15 | }, 500) 16 | } 17 | 18 | sendMsg(); -------------------------------------------------------------------------------- /Pub_Sub/receive_logs.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const exchangeName = "logs"; 4 | 5 | const recieveMsg = async () => { 6 | const connection = await amqplib.connect('amqp://localhost'); 7 | const channel = await connection.createChannel(); 8 | await channel.assertExchange(exchangeName, 'fanout', {durable: false}); 9 | const q = await channel.assertQueue('', {exclusive: true}); 10 | console.log(`Waiting for messages in queue: ${q.queue}`); 11 | channel.bindQueue(q.queue, exchangeName, ''); 12 | channel.consume(q.queue, msg => { 13 | if(msg.content) console.log("THe message is: ", msg.content.toString()); 14 | }, {noAck: true}) 15 | } 16 | 17 | recieveMsg(); -------------------------------------------------------------------------------- /headers/emit_log_header.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const exchangeName = "header_logs"; 4 | const args = process.argv.slice(2); 5 | const msg = args[0] || 'Subscribe, Like, Comment'; 6 | 7 | const sendMsg = async () => { 8 | const connection = await amqplib.connect('amqp://localhost'); 9 | const channel = await connection.createChannel(); 10 | await channel.assertExchange(exchangeName, 'headers', {durable: false}); 11 | channel.publish(exchangeName, '', Buffer.from(msg), {headers:{account: 'old', method: 'github'}}); 12 | console.log('Sent: ', msg); 13 | setTimeout(() => { 14 | connection.close(); 15 | process.exit(0); 16 | }, 500) 17 | } 18 | 19 | sendMsg(); -------------------------------------------------------------------------------- /topics/emit_log_topic.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const exchangeName = "topic_logs"; 4 | const args = process.argv.slice(2); 5 | const msg = args[1] || 'Subscribe, Like, Comment'; 6 | const key = args[0] 7 | 8 | console.log(args, msg); 9 | 10 | const sendMsg = async () => { 11 | const connection = await amqplib.connect('amqp://localhost'); 12 | const channel = await connection.createChannel(); 13 | await channel.assertExchange(exchangeName, 'topic', {durable: false}); 14 | channel.publish(exchangeName, key, Buffer.from(msg)); 15 | console.log('Sent: ', msg); 16 | setTimeout(() => { 17 | connection.close(); 18 | process.exit(0); 19 | }, 500) 20 | } 21 | 22 | sendMsg(); -------------------------------------------------------------------------------- /routing/emit_log_direct.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const exchangeName = "direct_logs"; 4 | const args = process.argv.slice(2); 5 | const msg = args[1] || 'Subscribe, Like, Comment'; 6 | const logType = args[0] 7 | 8 | console.log(args, msg); 9 | 10 | const sendMsg = async () => { 11 | const connection = await amqplib.connect('amqp://localhost'); 12 | const channel = await connection.createChannel(); 13 | await channel.assertExchange(exchangeName, 'direct', {durable: false}); 14 | channel.publish(exchangeName, logType, Buffer.from(msg)); 15 | console.log('Sent: ', msg); 16 | setTimeout(() => { 17 | connection.close(); 18 | process.exit(0); 19 | }, 500) 20 | } 21 | 22 | sendMsg(); -------------------------------------------------------------------------------- /work_queues/consume_task.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const queueName = "task"; 4 | 5 | const consumeTask = async () => { 6 | const connection = await amqplib.connect('amqp://localhost'); 7 | const channel = await connection.createChannel(); 8 | await channel.assertQueue(queueName, {durable: true}); 9 | channel.prefetch(1); 10 | console.log(`Waiting for messages in queue: ${queueName}`); 11 | channel.consume(queueName, msg => { 12 | const secs = msg.content.toString().split('.').length - 1; 13 | console.log("[X] Received:", msg.content.toString()); 14 | setTimeout(() => { 15 | console.log("Done resizing image"); 16 | channel.ack(msg); 17 | }, secs * 1000); 18 | }, {noAck: false}) 19 | } 20 | 21 | consumeTask(); -------------------------------------------------------------------------------- /headers/receive_logs_header_all.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const exchangeName = "header_logs"; 4 | 5 | const recieveMsg = async () => { 6 | const connection = await amqplib.connect('amqp://localhost'); 7 | const channel = await connection.createChannel(); 8 | await channel.assertExchange(exchangeName, 'headers', {durable: false}); 9 | const q = await channel.assertQueue('', {exclusive: true}); 10 | console.log(`Waiting for messages in queue: ${q.queue}`); 11 | 12 | channel.bindQueue(q.queue, exchangeName, '', {'account': 'new', 'method': 'google', 'x-match': 'all'}); 13 | 14 | channel.consume(q.queue, msg => { 15 | if(msg.content) console.log(`Routing Key: ${JSON.stringify(msg.properties.headers)}, Message: ${msg.content.toString()}`); 16 | }, {noAck: true}) 17 | } 18 | 19 | recieveMsg(); -------------------------------------------------------------------------------- /headers/receive_logs_header_any.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const exchangeName = "header_logs"; 4 | 5 | const recieveMsg = async () => { 6 | const connection = await amqplib.connect('amqp://localhost'); 7 | const channel = await connection.createChannel(); 8 | await channel.assertExchange(exchangeName, 'headers', {durable: false}); 9 | const q = await channel.assertQueue('', {exclusive: true}); 10 | console.log(`Waiting for messages in queue: ${q.queue}`); 11 | 12 | channel.bindQueue(q.queue, exchangeName, '', {'account': 'new', 'method': 'facebook', 'x-match': 'any'}); 13 | 14 | channel.consume(q.queue, msg => { 15 | if(msg.content) console.log(`Routing Key: ${JSON.stringify(msg.properties.headers)}, Message: ${msg.content.toString()}`); 16 | }, {noAck: true}) 17 | } 18 | 19 | recieveMsg(); -------------------------------------------------------------------------------- /topics/receive_logs_topic.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const args = process.argv.slice(2); 4 | 5 | if (args.length == 0) { 6 | console.log("Usage: receive_logs_topic.js ."); 7 | process.exit(1); 8 | } 9 | 10 | 11 | const exchangeName = "topic_logs"; 12 | 13 | const recieveMsg = async () => { 14 | const connection = await amqplib.connect('amqp://localhost'); 15 | const channel = await connection.createChannel(); 16 | await channel.assertExchange(exchangeName, 'topic', {durable: false}); 17 | const q = await channel.assertQueue('', {exclusive: true}); 18 | console.log(`Waiting for messages in queue: ${q.queue}`); 19 | args.forEach(function(key) { 20 | channel.bindQueue(q.queue, exchangeName, key); 21 | }); 22 | channel.consume(q.queue, msg => { 23 | if(msg.content) console.log(`Routing Key: ${msg.fields.routingKey}, Message: ${msg.content.toString()}`); 24 | }, {noAck: true}) 25 | } 26 | 27 | recieveMsg(); -------------------------------------------------------------------------------- /rpc/rpc_server.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const queueName = "rpc_queue"; 4 | 5 | function fibonacci(n) { 6 | if (n == 0 || n == 1) 7 | return n; 8 | else 9 | return fibonacci(n - 1) + fibonacci(n - 2); 10 | } 11 | 12 | const processTask = async () => { 13 | const connection = await amqplib.connect('amqp://localhost'); 14 | const channel = await connection.createChannel(); 15 | await channel.assertQueue(queueName, {durable: false}); 16 | channel.prefetch(1); 17 | console.log(' [x] Awaiting RPC requests'); 18 | 19 | channel.consume(queueName, msg => { 20 | const n = parseInt(msg.content.toString()); 21 | console.log(" [.] fib(%d)", n); 22 | 23 | const fibNum = fibonacci(n); 24 | 25 | channel.sendToQueue(msg.properties.replyTo, Buffer.from(fibNum.toString()), { 26 | correlationId: msg.properties.correlationId 27 | }); 28 | 29 | channel.ack(msg); 30 | 31 | }, {noAck: false}) 32 | } 33 | 34 | processTask(); -------------------------------------------------------------------------------- /routing/receive_logs_direct.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | 3 | const args = process.argv.slice(2); 4 | 5 | if (args.length == 0) { 6 | console.log("Usage: receive_logs_direct.js [info] [warning] [error]"); 7 | process.exit(1); 8 | } 9 | 10 | 11 | const exchangeName = "direct_logs"; 12 | 13 | const recieveMsg = async () => { 14 | const connection = await amqplib.connect('amqp://localhost'); 15 | const channel = await connection.createChannel(); 16 | await channel.assertExchange(exchangeName, 'direct', {durable: false}); 17 | const q = await channel.assertQueue('', {exclusive: true}); 18 | console.log(`Waiting for messages in queue: ${q.queue}`); 19 | args.forEach(function(severity) { 20 | channel.bindQueue(q.queue, exchangeName, severity); 21 | }); 22 | channel.consume(q.queue, msg => { 23 | if(msg.content) console.log(`Routing Key: ${msg.fields.routingKey}, Message: ${msg.content.toString()}`); 24 | }, {noAck: true}) 25 | } 26 | 27 | recieveMsg(); -------------------------------------------------------------------------------- /rpc/rpc_client.js: -------------------------------------------------------------------------------- 1 | const amqplib = require('amqplib'); 2 | const { v4: uuidvv4} = require('uuid') 3 | 4 | const args = process.argv.slice(2); 5 | 6 | if (args.length == 0) { 7 | console.log("Usage: rpc_client.js num"); 8 | process.exit(1); 9 | } 10 | 11 | const num = parseInt(args[0]); 12 | const uuid = uuidvv4(); 13 | 14 | const getFib = async () => { 15 | const connection = await amqplib.connect('amqp://localhost'); 16 | const channel = await connection.createChannel(); 17 | const q = await channel.assertQueue('', {exclusive: true}); 18 | 19 | console.log(' [x] Requesting fib(%d)', num); 20 | 21 | channel.sendToQueue('rpc_queue', Buffer.from(num.toString()), { 22 | replyTo: q.queue, 23 | correlationId: uuid 24 | }); 25 | 26 | channel.consume(q.queue, msg => { 27 | if(msg.properties.correlationId == uuid){ 28 | console.log(' [.] Got %s', msg.content.toString()); 29 | setTimeout(() => { 30 | connection.close(); 31 | process.exit(0); 32 | }, 500) 33 | } 34 | }, {noAck: true}); 35 | } 36 | 37 | getFib(); --------------------------------------------------------------------------------