Installation and connection

Start RabbitMQ, install the Node.js client, and verify the connection.

🎯 Introduction

Start RabbitMQ, install the Node.js client, and verify the connection.


🧱 Server and client

The RabbitMQ server receives and stores messages and needs to keep running. A client library connects your application to that server. This course runs the server in Docker and uses amqplib for Node.js programs.

Docker runs services in containers, which you can think of as separate runtime environments. Install and start Docker first; see the Docker course for the basics. This course pins Node.js 22.9.0, RabbitMQ 4.1.4-management, and amqplib 0.10.8 as a reproducible practice setup, rather than a list of the latest versions.


🛠 Start the server

docker run -d --name rabbitmq-course --hostname rabbitmq-course \
    -p 127.0.0.1:5672:5672 \
    -p 127.0.0.1:15672:15672 \
    -e RABBITMQ_DEFAULT_USER=learner \
    -e RABBITMQ_DEFAULT_PASS=learn-local-only \
    -v rabbitmq-course-data:/var/lib/rabbitmq \
    rabbitmq:4.1.4-management

Programs connect on port 5672; the browser management UI uses 15672. Binding to 127.0.0.1 restricts access to your local machine. The management image includes the management UI. These credentials are for local practice. The rabbitmq-course-data volume stores container data and will be used later to test restarting the server.

If the named container already exists, start it with docker start rabbitmq-course instead of creating it again. Check readiness:

docker exec rabbitmq-course rabbitmq-diagnostics -q ping

Continue after Ping succeeded appears. If the check fails just after startup, wait a few seconds and retry.


📦 Prepare the practice directory

The following setup uses Node.js 22.9.0 and amqplib 0.10.8:

mkdir rabbitmq-payment
cd rabbitmq-payment
npm init -y
npm install --save-exact amqplib@0.10.8

Save all subsequent files in this directory and run terminal commands there. You can also download the complete examples, extract them, and run the same installation command in that directory.

Create a shared connection configuration for subsequent files:

config.mjs
export const rabbitmqUrl = process.env.RABBITMQ_URL ?? 'amqp://learner:learn-local-only@localhost:5672';

amqp:// specifies the messaging protocol, followed by the username, password, host, and port. AMQP defines how programs exchange messages with RabbitMQ; this course uses AMQP 0-9-1. The RABBITMQ_URL environment variable overrides the default URL.

Create a connection check:

check.mjs
import amqp from 'amqplib';
import { rabbitmqUrl } from './config.mjs';

const connection = await amqp.connect(rabbitmqUrl);
console.log('Connected successfully');
await connection.close();

Run it:

node check.mjs

The program prints “Connected successfully” and exits. A connection is a network session between your program and RabbitMQ. Close it when finished; a program that continuously receives messages needs to keep it open.


❓ Questions and answers

Q1: What should I check first if the connection fails?

Run the server health check, then verify the host, port 5672, and credentials. Port 15672 is for the management UI and cannot be used for this connection.

Q2: Why does login still fail after changing the credential environment variables?

The default user is created on the first startup with an empty data directory. An existing volume does not recreate the user. Use the original credentials or update the user through the management tools.

Q3: Do I need to install dependencies for every article?

No. All chapters share this directory and its client dependency.


🧪 Exercise

Run docker stop rabbitmq-course, then run the check program and observe the connection failure. Start the container again, wait for the health check to pass, and verify that the connection works again.


🎉 Your client is connected to the server. You are ready to send messages!