RdKafka\ProducerTopic::produce

(PECL rdkafka >= 0.9.1)

RdKafka\ProducerTopic::produceProduce and send a single message

Description

public RdKafka\ProducerTopic::produce ( integer $partition , integer $msgflags [, string $payload = NULL [, string $key = NULL [, string $opaque = NULL ]]] ) : void

Produce and send a single message to broker.

This is async and non-blocking.

Note:

Since producing is asynchronous, you should call flush before you destroy the producer. Otherwise, any outstanding messages will be silently discarded.

Parameters

partition

Can be either RD_KAFKA_PARTITION_UA (unassigned) for automatic partitioning using the topic's partitioner function (see RdKafka\TopicConf::setPartitioner(), or a fixed partition (0..N).

msgflags

Must be either 0 or RD_KAFKA_MSG_F_BLOCK

payload

Payload string

key

Optional message key, if non-NULL it will be passed to the topic partitioner as well as be sent with the message to the broker and passed on to the consumer.

opaque

An optional application-provided per-message opaque string that will be provided in the message's delivary callback (see RdKafka\Conf::setDrMsgCb()). (Since PECL rdkafka 5.0.0 with librdkafka >= 1.0.0.)

Return Values

Returns no value.

Errors/Exceptions

Changelog

Version Description
5.0.0 Added optional opaque parameter.

Examples

Example #1 RdKafka\ProducerTopic::produce() example

<?php
$message 
= [
    
'type' => 'account-created',
    
'id' => $accountId,
    
'date' => date(DATE_W3C),
];

$payload json_encode($message);

$topic->produce(RD_KAFKA_PARTITION_UA0$payload$accountId);
?>