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.
Fun Things RabbitMQ Can Do
-
Talk to Many Friends:
- It can speak different languages like AMQP, STOMP, and MQTT.
-
Keeps Messages Safe:
- It remembers messages so they don’t get lost.
- It checks if the message got delivered.
-
Chooses Where Messages Go:
- It can send messages to the right boxes (queues).
-
Works Together:
- Lots of bunnies (nodes) can work as a team.
-
Never Stops:
- It can copy messages to keep them safe if something breaks.
-
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:
- Get Erlang from its website.
- Get RabbitMQ from its website.
- Set up RabbitMQ on your computer.
- Start RabbitMQ with this magic spell:
rabbitmq-server start
Important RabbitMQ Words
-
Producer:
- The friend that sends messages.
-
Exchange:
- The helper that figures out where the message should go.
-
Queue:
- The mailbox where messages wait.
-
Consumer:
- The friend that gets the messages from the mailbox.
-
Binding:
- A string that connects the exchange to the mailbox.
-
Message Acknowledgment:
- A way to say, “I got the message!”
Types of Message Routes
-
Direct Route:
- Sends messages with a specific name.
-
Fanout Route:
- Sends messages to everyone!
-
Topic Route:
- Sends messages to groups based on tags.
-
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
- Use strong mailboxes (queues) for important messages.
- Watch RabbitMQ to make sure it’s working.
- Don’t take too many messages at once.
- Use a special mailbox (dead-letter) for failed messages.
- Change passwords often to stay safe.
Learn More
RabbitMQ is a friendly helper to move messages around. Give it a try!