0
0
RabbitMQdevops~5 mins

Publishing messages in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
Publishing messages means sending data from one program to a message broker so other programs can receive and process it. This helps different parts of a system talk to each other without being directly connected.
When you want to send a task to a worker program to process later.
When you need to notify multiple services about an event, like a new user signup.
When you want to decouple parts of your system so they can work independently.
When you want to handle spikes in requests by queuing messages.
When you want to ensure messages are not lost even if a service is temporarily down.
Config File - publish_message.py
publish_message.py
import pika

# Connect to RabbitMQ server on localhost
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# Declare a queue named 'task_queue' to send messages to
channel.queue_declare(queue='task_queue', durable=True)

# The message to send
message = 'Hello RabbitMQ!'

# Publish the message to the queue
channel.basic_publish(
    exchange='',
    routing_key='task_queue',
    body=message,
    properties=pika.BasicProperties(
        delivery_mode=2  # make message persistent
    ))
print(" [x] Sent 'Hello RabbitMQ!'")

# Close the connection
connection.close()

This Python script connects to a RabbitMQ server running locally.

It declares a queue named task_queue to ensure it exists before sending messages.

The message Hello RabbitMQ! is sent to the queue with persistence enabled so it won't be lost if RabbitMQ restarts.

Finally, the connection is closed cleanly.

Commands
Run the Python script to connect to RabbitMQ and send a message to the 'task_queue'. This starts the publishing process.
Terminal
python3 publish_message.py
Expected OutputExpected
[x] Sent 'Hello RabbitMQ!'
Check the RabbitMQ server to see the list of queues and confirm that 'task_queue' exists and has messages.
Terminal
rabbitmqctl list_queues
Expected OutputExpected
Listing queues ... task_queue 1
Key Concept

If you remember nothing else from this pattern, remember: publishing messages means sending data to a queue so other programs can process it later without direct connection.

Common Mistakes
Not declaring the queue before publishing messages.
If the queue does not exist, the message will be lost or cause an error.
Always declare the queue with the same name and durability settings before sending messages.
Not setting message persistence for important messages.
Messages may be lost if RabbitMQ restarts without persistence.
Set delivery_mode=2 in message properties to make messages persistent.
Closing the connection before the message is fully sent.
The message might not reach the server if the connection closes too early.
Publish the message first, then close the connection.
Summary
Use a client library to connect to RabbitMQ and declare the queue before sending messages.
Publish messages to the queue with persistence if you want them saved on server restarts.
Verify the queue and message count using RabbitMQ management commands.