/** * Starts consuming messages */ public async consume(): Promise<void> { // Not absolutely necessary but enforces user to call connect() before calling consume() this.checkReadiness() await this.consumer.run({ autoCommit: false, eachMessage: async (payload) => { onMessageReceived(payload, this) } }) }
/** * Connects to Kafka and subscribes to each topic * * @returns a Promise that resolves if the connection is successful and rejects otherwise */ public async connect(): Promise<void> { await this.consumer.connect() for (const topic of this.topics) { await this.consumer.subscribe({ topic }) } this.isReady = true }
/** * Instantiates a new Kafka client * * @param brokers - List of brokers to connect to * @param kafkaConfig - Kafka client configuration */ protected constructor(kafkaConfig: KakfaJS.KafkaConfig) { super() this.brokers = kafkaConfig.brokers this.kafka = new KakfaJS.Kafka({ clientId: 'orchestrate-sdk', ...kafkaConfig }) }
async function run() { await producer.connect(); await consumer.connect(); // receive certification service response await consumer.subscribe({ topic: 'certification-response' }); await consumer.run({ eachMessage: async ({ topic, partition, message }) => { console.log('Response', String(message.value)); }, }); app.listen(3333); }
/** * Creates a new instance of the Producer * * @param brokers - List of brokers to connect to * @param kafkaConfig - Kafka client configuration * @param producerConfig - Producer configuration */ constructor( brokers: string[], kafkaConfig?: Omit<KakfaJS.KafkaConfig, 'brokers'>, producerConfig?: KakfaJS.ProducerConfig ) { super({ clientId: 'orchestrate-sdk-producer', ...kafkaConfig, brokers }) this.producer = this.kafka.producer(producerConfig) }
router.post("/certifications", async (req, res) => { const message = { user: { id: 1, name: 'Vagner Zampieri' }, course: 'Kafka with Node.js' }; await req.producer.send({ topic: 'issue-certificate', compression: CompressionTypes.GZIP, messages: [ { value: JSON.stringify(message) }, { value: JSON.stringify({ ...message, user: { ...message.user, name: 'Jesus' } }) }, ] }); return res.json({ ok: true }); });
const consumeProductStream = async (handler) => { await consumer.connect() await consumer.subscribe({ topic: 'products', fromBeginning: false }) await consumer.run({ eachMessage: async ({ topic, partition, message }) => { console.log('received message') try { await handler(JSON.parse(message.value.toString())) } catch (e) { console.error('unable to handle incoming message', e) } }, }) }
/** * Commits the offsets specified by the message * * @param message - Message from which to get the offset */ public async commit(message: IResponse): Promise<void> { this.checkReadiness() await this.consumer.commitOffsets([ { offset: (parseInt(message.offset, 10) + 1).toString(), topic: message.topic, partition: message.partition } ]) }
/** * Send a record to a Kafka topic with new image version details. * * @param image the image * @returns {Promise<void>} */ async notify(image) { const producer = this.kafka.producer(); await producer.connect(); return producer.send({ topic: this.configuration.topic, messages: [{ value: JSON.stringify(image) }], }); }
/** * Disconnects from the broker and unsubscribes from the topics * * @returns a Promise that resolves if the connection is disconnected successfully */ public async disconnect(): Promise<void> { this.checkReadiness() await this.consumer.disconnect() this.removeAllListeners() this.isReady = false }
/** * Creates a new instance of the Consumer * * @param brokers - List of brokers to connect to * @param topics - List of topics to consume * @param kafkaConfig - Kafka client configuration * @param consumerConfig - Consumer configuration */ constructor( brokers: string[], topics = [DEFAULT_TOPIC_TX_DECODED, DEFAULT_TOPIC_TX_RECOVER], kafkaConfig?: Omit<KakfaJS.KafkaConfig, 'brokers'>, consumerConfig?: KakfaJS.ConsumerConfig ) { super({ clientId: 'orchestrate-sdk-consumer', ...kafkaConfig, brokers }) this.topics = topics this.consumer = this.kafka.consumer({ groupId: 'orchestrate-sdk-consumer', ...consumerConfig }) }