Skip to content

Sending

Robin Harvey edited this page Sep 25, 2011 · 9 revisions

Chapter 3 - Sending messages.

Amqp applications that send messages are called "producers", although a single application can both produce and consume messages. Producing means sending a message to the broker, there are ways of getting the broker to respond either in cases where a message fails or cannot be immediately delivered, or by acknowledging message receipt.

Basic Usage

The basic steps to publish a message follow the same pattern as the steps to send any Amqp message, the only difference is that you specify the message content as the third parameter to the \amqphp\Channel factory:

// $chan is an \amqphp\Channel object
$params = array('content-type' => 'text/plain',
    'content-encoding' => 'UTF-8',
    'routing-key' => 'my-rk',
    'exchange' => 'my-ex');
$message = $chan->basic('publish', $params, '..message content..');
$chan->invoke($message);

By default, Amqp messages are one-way, you send messages at will and the broker silently receives and processes these. If a message fails, or cannot be routed, by default the broker will simply discard the message. This is undesirable in many cases, and you can change this by using either the mandatory or immediate parameters (i.e. in $params above), here's how the Amqp spec defines these:

  • mandatory This flag tells the server how to react if the message cannot be routed to a queue. If this flag is set, the server will return an unroutable message with a Return method. If this flag is zero, the server silently drops the message.
  • immediate This flag tells the server how to react if the message cannot be routed to a queue consumer immediately. If this flag is set, the server will return an undeliverable message with a Return method. If this flag is zero, the server will queue the message, but with no guarantee that it will ever be consumed.

In both cases, messages that can't be delivered as specified are returned with a "basic.return" method. In order to receive these, you need to consume, i.e. wait for a period of time to see if anything comes back (see chapter 5 for more detail on how to do that). Returned messages are delivered to the ChannelEventHandler->publishReturn() method, this object is added to the Channel by calling amqphp\Channel->setEventHandler(). Another way to receive message delivery notification is to use a RabbitMQ- specific extension, which is detailed in chapter 6.

Previous Chapter : API Overview

Next Chapter : A simple message reader

Documentation home

Clone this wiki locally