RabbitMQ Javascript Cheat-Sheet

November 8, 20252 min read

RabbitMQ Javascript Cheat-Sheet

RabbitMQ Docker setup

Run RabbitMQ with the management plugin enabled:

docker run -d \
  --name rabbitmq \
  -p 5672:5672 \
  -p 15672:15672 \
  rabbitmq:management

The connexion string is amqp://localhost:5672

You can connect to your instance using RabbitGUI at http://localhost:15672 (guest/guest)

Rabbitgui connexion screen

Docker compose setup for RabbitMQ

Create a docker-compose.yml file with custom credentials and persistent storage:

version: '3'
services:
  rabbitmq:
    image: rabbitmq:management
    container_name: rabbitmq
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: user
      RABBITMQ_DEFAULT_PASS: password
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
 
volumes:
  rabbitmq_data:

The connexion string is amqp://user:password@localhost:5672

You can connect to your instance using RabbitGUI at http://localhost:15672 (user/password)

Installation in NodeJs

Add the amqplib package to your project:

npm install amqplib

Producer example

You can publish directly to a queue using the sendToQueue method:

import amqp from "amqplib";
 
const connection = await amqp.connect("amqp://localhost:5672");
const channel = await connection.createChannel();
await channel.assertQueue("my_queue");
 
channel.sendToQueue("my_queue", Buffer.from("Hello RabbitGUI!"));
 
await channel.close();
await connection.close();

Consumer example

Your consumer can listen to messages from a queue using the consume method:

import amqp from "amqplib";
 
const connection = await amqp.connect("amqp://localhost:5672");
const channel = await connection.createChannel();
await channel.assertQueue("my_queue");
 
channel.consume("my_queue", (msg) => {
  console.log("Message", msg.content.toString());
}, { noAck: true });

Exchanges examples

Direct

A direct exchange routes messages to queues based on exact routing key matches:

await channel.assertExchange("my_exchange", "direct");
await channel.assertQueue("my_queue");
await channel.bindQueue("my_queue", "my_exchange", "my.routing.key");
 
channel.publish("my_exchange", "my.routing.key", Buffer.from("Hello RabbitGUI!"));
 
channel.consume("my_queue", (msg) => {
  console.log("Message", msg.content.toString());
});

Fanout

A fanout exchange broadcasts messages to all bound queues, ignoring routing keys:

await channel.assertExchange("my_exchange", "fanout");
await channel.assertQueue("my_queue");
await channel.bindQueue("my_queue", "my_exchange", "");
 
channel.publish("my_exchange", "", Buffer.from("Hello RabbitGUI!"));
 
channel.consume("my_queue", (msg) => {
  console.log("Message", msg.content.toString());
});

Topic

A topic exchange routes messages based on wildcard pattern matching (* for one word, # for zero or more words):

await channel.assertExchange("my_exchange", "topic");
await channel.assertQueue("my_queue");
await channel.bindQueue("my_queue", "my_exchange", "*.routing.*");
 
channel.publish("my_exchange", "my.routing.key", Buffer.from("Hello RabbitGUI!"));
 
channel.consume("my_queue", (msg) => {
  console.log("Message", msg.content.toString());
});

Message acknowledgements

Automatic acknowledgement

RabbitMQ will dequeue messages as soon as they’ve been sent down the wire.

channel.consume("my_queue", (msg) => {
  //...
}, { noAck: true });

Manual acknowledgement

Manually acknowledge messages after processing to ensure they are removed from the queue:

channel.consume("my_queue", (msg) => {
  //...
  channel.ack(msg)
});

Reject and requeue

Reject a message and put it back in the queue for reprocessing:

channel.consume("my_queue", (msg) => {
  //...
  channel.nack(msg)
});

Reject and do not requeue

Reject a message and discard it (sends to dead letter queue if configured):

channel.consume("my_queue", (msg) => {
  //...
  channel.nack(msg, false, false)
});

Dead Letter Queues

Configure a queue with a dead letter exchange to catch rejected or expired messages:

import amqp from "amqplib";
 
const connection = await amqp.connect("amqp://localhost:5672");
const channel = await connection.createChannel();
 
// Create the dead letter exchange and queue
await channel.assertExchange("dlx_exchange", "direct");
await channel.assertQueue("dead_letter_queue");
await channel.bindQueue("dead_letter_queue", "dlx_exchange", "");
 
// Create main queue with dead letter configuration
await channel.assertQueue("my_queue", {
  deadLetterExchange: "dlx_exchange",
  deadLetterRoutingKey: ""
});

More articles about RabbitMQ

How to log into your CloudAMQP RabbitMQ instanceHow to log into your CloudAMQP RabbitMQ instanceUse RabbitGUI to connect to your CloudAMQP instance and manage your dead letter queues with easeHow security is built into RabbitGUIHow security is built into RabbitGUIRabbitGUI was built with security as a top priority for its users, and here is how it was done!A better RabbitMQ UIA better RabbitMQ UIA geat experience is made out of many small details, have a look at all the features we packed into RabbitGUI to make your life easier

RabbitGUI, the missing RabbitMQ IDE

Debug, monitor, and manage RabbitMQ with a modern developer interface.

Try nowRabbitGUI screenshot