Streamlining Messaging with Platformatic RabbitMQ hooks

Streamlining Messaging with Platformatic RabbitMQ hooks

Secure and reliable messaging between components is crucial in maintaining communication between the different components in a microservice architecture. RabbitMQ is a popular messaging broker that handles asynchronous messaging in microservice applications. With Platformatic RabbitMQ hooks, you can integrate RabbitMQ into your Platformatic applications.

In this article, you will learn how to use RabbitMQ, a messaging broker, and integrate it with Platformatic’s RabbitMQ Hooks Stackable. You will see how you can simplify and enhance message-driven processes in modern applications, focusing on the seamless integration of the RabbitMQ Hooks Stackable.

A complete code version is available on GitHub.


To follow this tutorial, make sure to have the following prerequisites:

What is Platformatic RabbitMQ hooks?

Platformatic RabbitMQ hooks allow you to wrap RabbitMQ inside your Platformatic application and export messages published on an exchange to an HTTP endpoint or publish them to an exchange.

RabbitMQ hook is available as a stackable template within the stackable marketplace. It is the wrapper for RabbitMQ and your Platformatic application.

What is a Stackable?With Stackables, developers can take their code and configurations, encapsulate them into augmented npm modules, and publish them on their internal npm registry. Stackables templates come fully loaded with everything your team needs, including automatic updates, and allow for the simple layering of your specific configurations. The Stackables Marketplace is a one-stop destination for reusing, composing, and publishing microservices and templates

Creating your RabbitMQ hooks application

To create and run our RabbitMQ hooks application, we need to create a runtime with two Platformatic applications, one for the RabbitMQ hooks and another service for making your messaging application.

Run the command below and follow the steps:

npx create-platformatic@latest

And follow the prompts:

? What kind of project do you want to create? Application
? Where would you like to create your project? platformatic
Using existing configuration
? Which kind of project do you want to create? @platformatic/service
? What is the name of the service? handsome-combat
? Do you want to create another service? yes
? Which kind of project do you want to create? @platformatic/rabbitmq-hooks
? What is the name of the service? hoc-honeysuckle
? Do you want to create another service? no
? Which service should be exposed? handsome-combat
? Do you want to use TypeScript? no
? Do you want to initialize git? no

NOTE: Change the URL and directory name in the command to your application URL and name.

We created a Runtime with two Platformatic applications: the RabbitMQ hooks and a Platformatic service. We also exposed the service application as the entrypoint for your runtime, in this case, the handsome-combat service application from your prompt.

Now navigate to the .env file in the root of the platformatic application and update the SERVER_HOSTNAME, RABBITMQ_CONNECTION_URL, and RABBITMQ_TARGET_URL_0 as shown:

// rabbit-mq-poc/.env
PLT_HOC_HONEYSUCKLE_RABBITMQ_CONNECTION_URL=amqp://localhost:5672 # RabbitMQ connection URL with PORT connection

We specified the platformatic server hostname as your localhost, and added the connection URL to match your PORT for RabbitMQ connection, although this is optional.

Starting RabbitMQ with Docker

Before starting your application, you must start the RabbitMQ container using Docker. To do this in the root of your application, create a new file docker-compose.yml and add the configuration:

    image: rabbitmq:3
    container_name: 'rabbitmq'
        - 5672:5672
        - 15672:15672

Note*: You can change your RabbitMQ incoming connection port on the* docker-compose.ymlfile, but remember to append it to theRABBITMQ_CONNECTION_URLin your.envfile.

Start your RabbitMQ with docker using the command:

docker compose up

Creating a client for the RabbitMQ application server

Your application is a Platformatic runtime with two services: the RabbitMQ hooks stackable and your service application. To create a client to invoke the RabbitMQ hooks from the service application, run the command:

npx platformatic client --runtime hoc-honeysuckle --name rabbit

This will create a rabbit folder in the handsome-combat application directory with three files:

  • package.json: This file contains a JSON object of the name and type of your client

  • rabbit.d.ts: This file contains the types and interfaces for your RabbitMQ hooks stackable, including the routes and endpoints from the RabbitMQ hooks.

  • rabbit.openapi.json: A JSON object of your client, including the RabbitMQ hooks endpoint.

NOTE: Change the URL and directory name in the command to your application URL and name.

Sending messages to RabbitMQ with Platformatic RabbitMQ hooks

To send and receive messages to the RabbitMQ server using Platformatic RabbitMQ hooks. You need to define a POST endpoint /send for sending messages and another for receiving messages. This will be done in the root.js file of the routes directory of your service application.

First, reference the client-server .ts file in your root.js as shown below:

/// <reference path="../rabbit/rabbit.d.ts" />

Next, update the root.js file by adding a POST endpoint:"/send", async function (request, reply) {
    await request.rabbit.publish({
      exchange: "my-exchange",
      routingKey: "my-queue",
    return reply.status(201).send({
      status: "Success",
      statusCode: 201,
      message: "Message successfully sent to RabbitMQ server.",

The code above defines a POST endpoint at /send. When a request is made, the function sends a message to the RabbitMQ server. It uses request.rabbit.publish from the RabbitMQ hooks to send a message to RabbitMQ, specifying the exchange and routingKey with the request body in the message.

After successfully publishing the message, the server responds with a 201 status code and a success message.

Receiving messages to RabbitMQ with Platformatic RabbitMQ hooks

Under the /send route, add a new Fastify route called /receive."/receive", async function (request, reply) {{ body: request.body });
    return reply.send({
      status: "Success",
      statusCode: 200,
      message: "Message successfully received from RabbitMQ server.",

In the receive route, the receives and logs all incoming messages from the RabbitMQ server and sends a status code of 200 status code and a JSON that says that the message was successfully received from the RabbitMQ server.

Start your server by running:

npm start

Open your application URL

Click on the OpenAPI Documentation button and send a message as shown below:

You can check your console to see the message received:

Wrapping Up

Integrating RabbitMQ with Platformatic using the RabbitMQ hooks offers a seamless solution for handling messages in Platformatic applications.

In this tutorial, we went over the step-by-step process of setting up RabbitMQ hooks in a Platformatic application, configuring Docker for RabbitMQ, creating a client-server with a Platformatic client, and creating endpoints for sending and receiving messages from RabbitMQ.

You can extend this application by creating another service that receives messages from the server.

Further resources: