How to manually publish messages to RabbitMQ

May 28, 20253 min read

How to manually publish messages to RabbitMQ

Why would you want to manually send messages to RabbitMQ?

Good question. Sometimes you just need to test something quickly, you want to trigger a specific job, or you want to retry a task that failed. All of those are valid reasons to manually publish a message to RabbitMQ. What sounds like a dead simple task is actually more painful than you might think. In this article we will go over some ways to publish messages directly to RabbitMQ, from painful to effortless using RabbitGUI.

Sending messages via code

While this is not the most practical solution, it is still a solution in case you have nothing else. To publish a message in RabbitMQ using JavaScript, you’ll typically use the amqplib library. First, install the library:

npm install amqplib

Then, run a script like this:

import amqplib from 'amqplib';
 
const conn = await amqplib.connect('amqp://localhost');
 
const ch = await conn.createChannel();
 
ch.publish(
  'my-exchange',
  'my.routing.key',
  Buffer.from(JSON.stringify({ foo: 'bar' }))
);

This example connects to RabbitMQ, creates a channel, and sends a JSON-encoded message to it. Pretty straight forward and might do the trick if you need to connect to an external API or do some computation before sending the messages.

Using a UI to send messages to RabbitMQ

The simplest approach is to use RabbitGUI, which lets you publish messages to a specific queue or exchange with just a few clicks and a full-fledged JSON editor. Go to any queue and go to the "Publish" tab:

Json editor to publish messages to rabbitmq

Compared to the built-in RabbitMQ management plugin, RabbitGUI provides quite a few quality-of-life improvements, such as :

  • An actual JSON editor (editor from VSCode) to help you highlight and format your messages using ⇧⌥F
  • The ability to select where to publish your message with which routing key based on available bindings
  • A history of all published messages, properly formatted and searchable

Alternatively, you can go to the "Publish" tab of an exchange and publish a message from there. The UI is similar, and you have autocompletion on the routing key based on the bindings of the exchange.

Ui to publish messages to rabbitmq

Note that the history of messages you publish is stored in RabbitGUI and shared across queues and exchanges, so you can easily find the messages you published in the past no matter where you sent it from.

You can learn more about RabbitGUI here.

Use RabbitMQ management plugin to publish messages

You can use the management plugin to publish messages to RabbitMQ. There are two ways to do so: either to a specific queue or to an exchange. To send a message to a specific queue, go to the "Queues" tab in the management plugin, select the queue you want to send a message to, and unfold the "Publish message" section. You can then enter the message body and properties:

Rabbitmq management plugin send message screenshot

Note that the message will be sent to the default exchange with the name of the queue as the routing key, which means that it will be routed to the queue you selected. If you want to send a message to an exchange, go to the "Exchanges" tab, select the exchange you want to send a message to, and unfold the "Publish message" section. You can then enter the message body and properties:

Rabbitmq send message to excahnge via management plugin

Unlike RabbitGUI, the management plugin does not provide a JSON editor, and it will not highlight syntax errors for you. It also does not provide autocompletion for the routing key, so you will have to know the routing key you want to use.

More articles about RabbitMQ

RabbitMQ: maximum size of a messageRabbitMQ: maximum size of a messageThe maximum size of a message in RabbitMQ is not defined by the protocol, but by the implementation. Unfortunately, this value is not well documented and has changed a lot over timeHow to visually explore RabbitMQ queue bindingsHow to visually explore RabbitMQ queue bindingsThere are many ways messages can be routed to queues in RabbitMQ, and it can be hard to understand how they are connected in a single placeDebugging policies in RabbitMQDebugging policies in RabbitMQWhile rules regarding which policies are applied to which queues are simple, visually understanding what is going on when something does not behave as expected might be tricky

RabbitGUI, the missing RabbitMQ IDE

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

Try nowRabbitGUI screenshot