Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import { Kafka, logLevel, KafkaConfig, SASLOptions } from "kafkajs";
const options: KafkaConfig = {
clientId: "my-client",
brokers: (process.env.KAFKA_BROKERS &&
process.env.KAFKA_BROKERS.split(",")) || ["localhost:9092"]
};
if (
process.env.KAFKA_AUTH_MECHANISM &&
process.env.KAFKA_AUTH_USERNAME &&
process.env.KAFKA_AUTH_PASSWORD
) {
let mechanism: string = "plain";
switch (process.env.KAFKA_AUTH_MECHANISM) {
case "plain":
mechanism = "plain";
break;
case "scram-sha-256":
mechanism = "scram-sha-256";
break;
case "scram-sha-512":
mechanism = "scram-sha-256";
break;
}
options.sasl = {
mechanism: mechanism,
username: process.env.KAFKA_AUTH_USERNAME,
password: process.env.KAFKA_AUTH_PASSWORD
};
}
if (process.env.DEBUG && process.env.DEBUG.includes('kafka')) {
options.logLevel = logLevel.DEBUG;
}
export default new Kafka(options);
export interface Message {
id: string;
[propName: string]: any;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment