Hands-on RabbitMQ Tutorial
Tags: rabbitmq, tutorial
Published: 2022-12-26 12:39:06

---
title: Hands-on RabbitMQ Tutorial
published: true
date: 2022-12-20 23:42:21 UTC
tags: rabbitmq,tutorial
canonical_url: https://avilpage.com/2022/12/hands-on-rabbitmq-tutorial.html
---

A short hands-on guide to get started with RabbitMQ for people who are in a hurry.

#### What is RabbitMQ?

![RabbitMQ](https://avilpage.com/images/rabbitmq-overview.png)

Image Credit: CloudAMQP

RabbitMQ<sup id="fnref:wikipedia-rabbitmq"><a href="https://avilpage.com/2022/12/hands-on-rabbitmq-tutorial.html#fn:wikipedia-rabbitmq">1</a></sup> is an open-source message broker software that implements the Advanced Message Queuing Protocol (AMQP). With RabbitMQ, producer and consumer applications can communicate asynchronously, and they will be completely decoupled.

#### RabbitMQ Terminology

**Producer** : A producer is a client that publishes messages to the RabbitMQ broker. Producers write data to exchanges.

**Consumer** : A consumer is a client that subscribes to queues and processes the messages. Consumers read data from queues.

**Queue** : A queue is a buffer that stores messages. A queue is bound to an exchange and receives messages from it.

**Exchange** : An exchange is a message routing agent that receives messages from producers and routes them to queues.

**Binding** : A binding is a link between an exchange and a queue. It is created with a routing key. The producer sends messages to the exchange with a routing key. The exchange routes the message to the queues that are bound with a matching routing key.

#### RabbitMQ Setup

We can use the official RabbitMQ docker image to run RabbitMQ locally. We can run the following command to start a RabbitMQ container:

```
$ docker run --rm --name=rabbitmq -p 15672:15672 -p 5672:5672 rabbitmq:3-management

```

This image has rabbitmq management plugin enabled. We can access the management UI at [http://localhost:15672](http://localhost:15672). The default username and password are both `guest`.

It also has `rabbitmqadmin` command line tool installed, which can manage RabbitMQ.

#### Passing Messages from UI

We can use the management UI to send and receive messages. We can create a new queue and exchange from the `Queues` section.

![RabbitMQ Queue](https://avilpage.com/images/rabbitmq-queue.png)

Once a queue is created, we can publish and consume messages from that queue.

![RabbitMQ Publish](https://avilpage.com/images/rabbitmq-publish.png)

#### Passing Messages from CLI

Instead of using web UI, we can use `rabbitmqadmin` CLI tool<sup id="fnref:rabbitmq-cli"><a href="https://avilpage.com/2022/12/hands-on-rabbitmq-tutorial.html#fn:rabbitmq-cli">2</a></sup> to send and receive messages. Let's create a topic exchange and a queue.

```
$ docker exec rabbitmq rabbitmqadmin declare exchange type=direct name=orders
# => exchange declared


$ docker exec rabbitmq rabbitmqadmin declare queue name=orders
# => queue declared

```

Let's publish a message to the exchange:

```
$ docker exec rabbitmq rabbitmqadmin publish routing\_key=orders payload='dummy message'
# => Message published

```

To receive messages from the queue, we can use the following command:

```
$ docker exec rabbitmq rabbitmqadmin get queue=orders

```

![RabbitMQ CLI](https://avilpage.com/images/rabbitmq-get-message.png)

#### Passing Messages from REST API

We can also use REST API to send and receive messages. Let's create a new exchange and queue:

```
$ curl -u guest:guest -X PUT -H "content-type:application/json" -d '{"type":"direct"}' http://localhost:15672/api/exchanges/%2f/orders


$ curl -u guest:guest -X PUT -H "content-type:application/json" -d '{"type":"topic", "durable": true}' http://localhost:15672/api/queues/%2f/orders

```

We can publish a message to the exchange:

```
$ curl -u guest:guest -X POST -H "content-type:application/json" -d '{"routing\_key":"orders","payload":"dummy message","payload\_encoding":"string", "properties": {} }' http://localhost:15672/api/exchanges/%2f/orders/publish

```

To receive messages from the queue, we can use the following command:

```
$ curl -u guest:guest -X GET http://localhost:15672/api/queues/%2f/orders/get

```

#### Conclusion

In this post, we have seen how to get started with RabbitMQ. We have seen how to use the management UI, CLI and REST API to send and receive messages.

* * *

1. [https://en.wikipedia.org/wiki/RabbitMQ](https://en.wikipedia.org/wiki/RabbitMQ) [↩](https://avilpage.com/2022/12/hands-on-rabbitmq-tutorial.html#fnref:wikipedia-rabbitmq "Jump back to footnote 1 in the text")

2. [https://www.rabbitmq.com/management-cli.html](https://www.rabbitmq.com/management-cli.html) [↩](https://avilpage.com/2022/12/hands-on-rabbitmq-tutorial.html#fnref:rabbitmq-cli "Jump back to footnote 2 in the text")