Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between assertQueue and send To Queue in RabbitMq

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});
  });
});
like image 345
Harry Avatar asked May 14 '18 05:05

Harry


People also ask

How does RabbitMQ queue work?

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.

What is RabbitMQ node?

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.

How do I use RabbitMQ with node js?

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.


2 Answers

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.

like image 197
user2768407 Avatar answered Oct 20 '22 00:10

user2768407


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

like image 33
venky07 Avatar answered Oct 20 '22 02:10

venky07