Skip to main content

Fiber and RabbitMQ example

Github StackBlitz

Description

This example demonstrates how to integrate RabbitMQ with a Fiber HTTP server. The API exposes a /send endpoint that publishes messages to a RabbitMQ queue. A separate worker process consumes messages from the queue and prints them to the console.

How it works

Architecture diagram

  • The Fiber API server connects to RabbitMQ and exposes GET /send?msg=<text>.
  • Each request publishes the msg query parameter as a message to the TestQueue queue.
  • The worker process subscribes to TestQueue and logs received messages.

Prerequisites

Environment variables

VariableDefaultDescription
RABBITMQ_URLamqp://user:password@localhost:5672/RabbitMQ connection URL

Setup

Start all services (RabbitMQ, worker, API) with a single command:

docker compose up --build

Option B: Manual setup

  1. Start RabbitMQ:
make docker.network
make docker.rabbitmq
  1. Wait ~30 seconds for RabbitMQ to be ready.

  2. Start the worker (in a separate terminal):

make docker.worker
  1. Run the API server:
make run
# or
RABBITMQ_URL=amqp://user:password@localhost:5672/ go run main.go

Endpoints

GET /send

Publishes a message to the TestQueue RabbitMQ queue.

ParameterTypeRequiredDescription
msgstringyesMessage to publish

Success response 200 OK:

{"status": "message sent"}

Error response 400 Bad Request:

{"error": "msg parameter required"}

curl examples

Send a message:

curl "http://127.0.0.1:3000/send?msg=Hello%20World"

Missing parameter (returns 400):

curl "http://127.0.0.1:3000/send"

Worker output

When a message is received, the worker prints:

2021/03/27 16:32:35 Successfully connected to RabbitMQ instance
2021/03/27 16:32:35 [*] - Waiting for messages
2021/03/27 16:32:35 [*] - Run Fiber API server and go to http://127.0.0.1:3000/send?msg=<YOUR TEXT HERE>
2021/03/27 16:33:24 Received message: Hello World

RabbitMQ management dashboard

The RabbitMQ management UI is available at http://localhost:15672 (default credentials: user / password).

RabbitMQ dashboard