Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Latest commit

 

History

History

knack-producer-client

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

knack-producer-client

A module for publishing records to Apache Kafka with integrated avro support.

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
});