What is RabbitMQ? || Devstream365 for programmer


What is RabbitMQ?

RabbitMQ is like a big helper bunny that moves messages from one place to another. It makes sure the messages get to the right place safely.

devstream365



Fun Things RabbitMQ Can Do

  1. Talk to Many Friends:

    • It can speak different languages like AMQP, STOMP, and MQTT.
  2. Keeps Messages Safe:

    • It remembers messages so they don’t get lost.
    • It checks if the message got delivered.
  3. Chooses Where Messages Go:

    • It can send messages to the right boxes (queues).
  4. Works Together:

    • Lots of bunnies (nodes) can work as a team.
  5. Never Stops:

    • It can copy messages to keep them safe if something breaks.
  6. Can Learn New Tricks:

    • You can add cool extras like monitors or password checks.

How to Use RabbitMQ

What You Need:

  • Erlang/OTP: A special tool RabbitMQ needs to work.
  • A computer with Linux, Windows, or macOS.

Steps:

  1. Get Erlang from its website.
  2. Get RabbitMQ from its website.
  3. Set up RabbitMQ on your computer.
  4. Start RabbitMQ with this magic spell:
    rabbitmq-server start
    

Important RabbitMQ Words

  1. Producer:

    • The friend that sends messages.
  2. Exchange:

    • The helper that figures out where the message should go.
  3. Queue:

    • The mailbox where messages wait.
  4. Consumer:

    • The friend that gets the messages from the mailbox.
  5. Binding:

    • A string that connects the exchange to the mailbox.
  6. Message Acknowledgment:

    • A way to say, “I got the message!”

Types of Message Routes

  1. Direct Route:

    • Sends messages with a specific name.
  2. Fanout Route:

    • Sends messages to everyone!
  3. Topic Route:

    • Sends messages to groups based on tags.
  4. Headers Route:

    • Sends messages based on secret notes (headers).

Commands You Can Use

Start RabbitMQ

rabbitmq-server

Add Fun Extras

rabbitmq-plugins enable <plugin_name>

Add Friends

  • Add a Friend:
    rabbitmqctl add_user <username> <password>
    
  • Remove a Friend:
    rabbitmqctl delete_user <username>
    
  • Give Permissions:
    rabbitmqctl set_permissions -p / <username> "<read_pattern>" "<write_pattern>" "<configure_pattern>"
    

Watch Messages

  • Turn on the monitor:
    rabbitmq-plugins enable rabbitmq_management
    
  • Look at the monitor at http://<server_ip>:15672.

Let’s Try It!

Sending Messages (Producer):

const amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', (err, connection) => {
  if (err) throw err;
  connection.createChannel((err, channel) => {
    if (err) throw err;
    const queue = 'hello';
    const message = 'Hello, RabbitMQ!';

    channel.assertQueue(queue, {
      durable: false
    });

    channel.sendToQueue(queue, Buffer.from(message));
    console.log(`Sent: ${message}`);
  });
});

Getting Messages (Consumer):

const amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', (err, connection) => {
  if (err) throw err;
  connection.createChannel((err, channel) => {
    if (err) throw err;
    const queue = 'hello';

    channel.assertQueue(queue, {
      durable: false
    });

    console.log(`Waiting for messages in ${queue}. To exit, press CTRL+C`);
    channel.consume(queue, (msg) => {
      console.log(`Received: ${msg.content.toString()}`);
    }, {
      noAck: true
    });
  });
});

Tips for Using RabbitMQ

  1. Use strong mailboxes (queues) for important messages.
  2. Watch RabbitMQ to make sure it’s working.
  3. Don’t take too many messages at once.
  4. Use a special mailbox (dead-letter) for failed messages.
  5. Change passwords often to stay safe.

Learn More


RabbitMQ is a friendly helper to move messages around. Give it a try!

Previous Post
No Comment
Add Comment
comment url