@optum/knack-producer-client
v1.4.1
Published
A module for producing records with Apache Kafka.
Downloads
3
Keywords
Readme
Usage
first things first...
$ npm i @optum/knack-producer-client [email protected]
options
- useHighLevelProducer:
[Boolean]
flag indicating to use the highlevel producer over the standard one - producerConfig:
[Object]
librd producer config - srOptions:
[Object]
: options to pass to knack-sr
Examples
simple
const KnackProducerClient = require("@optum/knack-producer-client");
const options = {
producerConfig: {
"metadata.broker.list": ["localhost:9092"]
},
srOptions: {
url: "http://localhost:8081"
}
};
const topic = "knack-test-topic-v1";
const key = { id: "1234" };
const value = { content: "from knack producer client" };
// await getting the producer so it lazy loads a connection the first time a message goes through
const producer = await KnackProducerClient.resolveInstance(options);
producer.publish({
topic,
key,
value
});
// calling for an instance with the same options with give back the same producer, so it will manage different connections for you
const queued = KnackProducerClient.resolveInstance(options).publish({
topic,
key,
value
});
also simple, but one more step
const KnackProducerClient = require("@optum/knack-producer-client");
const options = {
producerConfig: {
"metadata.broker.list": ["localhost:9092"]
},
srOptions: {
url: "http://localhost:8081"
}
};
const topic = "knack-test-topic-v1";
const key = { id: "1234" };
const value = { content: "from knack producer client" };
// connect at app start up or whenever
await KnackProducerClient.connectInstance(options);
// grab the producer through the instance method without needing to await it
const producer = KnackProducerClient.instance(options);
producer.publish({
topic,
key,
value
});