# Producer Client for producing messages to Kafka topics with support for idempotent production. The producer inherits from the [`Base`](./base.md) client. The complete TypeScript type of the `Producer` is determined by the `serializers` option. The producer supports idempotent production. ## Constructor Creates a new producer with type `Producer`. Options: | Property | Type | Description | | ----------------------- | ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `producerId` | `bigint` | Producer ID. | | `producerEpoch` | `number` | Producer epoch. | | `idempotent` | `boolean` | Idempotency of the producer. | | `acks` | `number` | Acknowledgement to wait before returning.

Valid values are defined in the `ProduceAcks` enumeration. | | `compression` | `string` | Compression algorithm to use before sending messages to the broker.

Valid values are: `snappy`, `lz4`, `gzip`, `zstd` | | `partitioner` | `(message: MessageToProduce) => number` | Partitioner to use to assign a partition to messages that lack it.

It is a function that receives a message and should return the partition number. | | `repeatOnStaleMetadata` | `boolean` | Whether to retry a produce operation when the system detects outdated topic or broker information.

Default is `true`. | | `serializers` | `Serializers` | Object that specifies which serialisers to use.

The object should only contain one or more of the `key`, `value`, `headerKey` and `headerValue` properties. | It also supports all the constructor options of `Base`. Notes: `zstd` is not available in node `v20` ## Basic Methods ### `send(options[, callback])` Sends one or more messages to Kafka. When `acks` is not `ProduceAcks.NO_RESPONSE`, then the return value is an object with the property `offsets` containing a list of written topic-partition-offset triplets. When `acks` is `ProduceAcks.NO_RESPONSE`, then the return value is an object with the property `unwritableNodes` containing a list of nodes which are currently busy and should wait for a `client:broker:drain` event before continuing. Options: | Property | Type | Description | | ---------- | ----------------------------------------------- | --------------------- | | `messages` | `MessageToProduce[]` | The messages to send. | It also accepts all options of the constructor except `serializers`. ### `close([callback])` Closes the producer and all its connections. The return value is `void`. ## Advanced Methods The producer manages auxiliary operations automatically. Some of the APIs are exposed to allow for advanced uses. ### `initIdempotentProducer(options[, callback])` Initialises an idempotent producer. It accepts all options of the constructor except `serializers`. The return value is an object containing the `producerId` and `producerEpoch` values returned from the broker.