I have found Example for Sending String in rabittMq and Receiving from the queue but I am not clear about about these methods - assertQueue , sendToQueue
send.js
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
var q = 'hello';
var msg = 'Hello World! - '+i;
ch.assertQueue(q, {durable: false});
ch.sendToQueue(q, new Buffer(msg));
console.log(" [x] Sent %s", msg);
});
setTimeout(function() { conn.close(); process.exit(0) }, 1000);
});
receive.js
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) { //amqp://localhost
conn.createChannel(function(err, ch) {
var q = 'hello';
ch.assertQueue(q, {durable: false});
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
ch.consume(q, function(msg) {
console.log(" [x] Received %s", msg.content.toString());
}, {noAck: true});
});
});
The user sends a PDF creation request to the web application. The web application (the producer) sends a message to RabbitMQ that includes data from the request such as name and email. An exchange accepts the messages from the producer and routes them to correct message queues for PDF creation.
A RabbitMQ cluster is a logical grouping of one or several nodes, each sharing users, virtual hosts, queues, exchanges, bindings, runtime parameters and other distributed state.
Create an account in their website and create an instance. Once you create an instance, click on the “RabbitMQ Manager” button. This should open the standard RabbitMQ admin console. Let us create a durable queue in the same console.
From your example,
assertQueue checks for "hello" queue, if it doesn't exist then it will create one.
sendToQueue put a message onto "hello" queue.
assertQueue is an declaration of queue and sendToQueue is pushing your message in the queue
To be noted : Declaring a queue is idempotent - it will only be created if it doesn't exist already
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With