RabbitMQ
JSON Message
Message Queuing
Software Development
Programming

How to send a JSON message with RabbitMQ (Python pika)

Codemia Knowledge Hub ·

Serialize the data to a JSON string with json.dumps and publish that string as the message body. RabbitMQ just carries the bytes; the producer and consumer agree that they are JSON. Below is a complete example in Python with the pika library.

The AMQP pieces involved

RabbitMQ follows the AMQP (Advanced Message Queuing Protocol) model:

  • Producer: an application that sends messages.
  • Queue: a buffer that stores messages.
  • Exchange: routes messages to one or more queues based on routing rules.
  • Binding: a link between a queue and an exchange.
  • Consumer: an application that receives messages.

Sending JSON with pika

Install pika

With a RabbitMQ server running locally or reachable remotely:

bash
pip install pika

Connect

Create a blocking connection to the broker and open a channel on it:

python
1import pika
2
3# Set up parameters based on your RabbitMQ configuration
4parameters = pika.ConnectionParameters('localhost')
5
6# Create a connection
7connection = pika.BlockingConnection(parameters)
8
9# Create a channel
10channel = connection.channel()

Serialize the message

Import the json module and turn the dictionary into a JSON string:

python
1import json
2
3# Create a dictionary that you want to send as JSON
4message = {
5    "type": "notification",
6    "message": "Hello RabbitMQ"
7}
8
9# Convert dictionary to JSON string
10message_json = json.dumps(message)

Declare the exchange and queue

Declare both before sending, then bind the queue to the exchange with a routing key:

python
1# Declare an exchange
2exchange_name = 'test_exchange'
3channel.exchange_declare(exchange=exchange_name, exchange_type='direct')
4
5# Declare a queue
6queue_name = 'test_queue'
7channel.queue_declare(queue=queue_name)
8
9# Bind the queue to the exchange
10channel.queue_bind(exchange=exchange_name, queue=queue_name, routing_key='test_key')

Publish

Publish the JSON string through the exchange with the same routing key, then close the connection:

python
1channel.basic_publish(exchange=exchange_name,
2                      routing_key='test_key',
3                      body=message_json)
4
5print("Sent JSON message: ", message_json)
6
7# Close connection
8connection.close()
ComponentDescriptionSample Code or Value
ProducerSends messages to an exchange.-
Exchange TypeTypes include direct, topic, headers, fanout.'direct'
Message FormatFormat of the message being sent.JSON serialized string
QueueStores messages until consumed.'test_queue'
pikaPython library to interact with RabbitMQ.import pika

Before using this in production

  • Errors and logging: handle disruptions such as network failures, and log deliveries and failures for diagnostics.
  • Durability and acknowledgements: configure message acknowledgements and queue durability to match your reliability needs so messages are not lost.
  • Security: enable SSL/TLS so data in transit is protected, especially when it is sensitive.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design