Message bodies and properties

Replace text with JSON and distinguish business fields from RabbitMQ message properties.

🎯 Introduction

Replace text with JSON and distinguish business fields from RabbitMQ message properties.


🧱 Why agree on a message format?

“Payment succeeded” alone does not tell the receiving system which order to update. The body needs to include the data the receiver needs to perform its operation.

The body is the data being sent. Properties describe it separately, such as its format in contentType and its identifier in messageId. RabbitMQ carries this information but does not verify the payment or validate JSON fields automatically.

Here, paymentId identifies the payment, orderId identifies the order, and amountFen is an integer amount in fen. These are application fields, not RabbitMQ requirements.


📤 Encode and send

Use a new queue, receipt, to avoid mixing JSON with the previous text messages:

send-json.mjs
import amqp from 'amqplib';
import { rabbitmqUrl } from './config.mjs';

const connection = await amqp.connect(rabbitmqUrl);
const channel = await connection.createChannel();

await channel.assertQueue('receipt', { durable: false });
const event = { paymentId: 'P1001', orderId: 'O1001', amountFen: 9900 };
channel.sendToQueue('receipt', Buffer.from(JSON.stringify(event)), {
    contentType: 'application/json',
    messageId: 'evt-P1001',
});
console.log('Submitted evt-P1001');
await channel.close();
await connection.close();

First encode the object as JSON text, then convert it to bytes. contentType labels the format without performing that conversion. messageId helps receivers correlate logs but does not automatically deduplicate messages.


📥 Read the body and properties

receive-json.mjs
import amqp from 'amqplib';
import { rabbitmqUrl } from './config.mjs';

const connection = await amqp.connect(rabbitmqUrl);
const channel = await connection.createChannel();

await channel.assertQueue('receipt', { durable: false });
await channel.consume('receipt', (message) => {
    if (message === null) return;
    const event = JSON.parse(message.content.toString());
    console.log(message.properties.messageId, event.orderId, event.amountFen);
}, { noAck: true });
console.log('Waiting for JSON messages');

Read the body through message.content and properties through message.properties. Start the receiver first, then send from another terminal:

node receive-json.mjs
node send-json.mjs

The receiver prints evt-P1001 O1001 9900. This example only receives valid JSON that we send ourselves. Real consumers also need to validate field types and required values. Rejecting malformed messages is covered in the failure handling stage.


🧰 Keep format changes compatible with existing messages

Queues may still contain older messages, and consumers may not all update at once. When adding an optional field, let receivers handle its absence. Before renaming or removing a required field, arrange consumer compatibility first. RabbitMQ does not automatically convert renamed fields.


❓ Questions and answers

Q1: Can I pass an object directly to sendToQueue?

No. This client method requires a Buffer body, so encode the object first.

Q2: Must the body be JSON?

No. The sender and receiver just need to agree on an encoding. This course uses JSON for readability.

Q3: Are different messages with the same messageId discarded?

No. The ordinary queues used in this course do not deduplicate by this property. Preventing duplicate business operations also requires consumer logic.


🧪 Exercise

Add and print an optional paidAt field, then send an older-format message without it. Both messages should be received successfully.


🎉 You can now read message bodies and their additional properties!