
KafkaJS Has a Free API That Brings Event Streaming to Any Node.js App
KafkaJS is the native Node.js client for Apache Kafka. Its API lets you produce, consume, and manage topics with pure JavaScript. Producer: Send Events import { Kafka } from " kafkajs " ; const kafka = new Kafka ({ clientId : " scraper " , brokers : [ " localhost:9092 " ] }); const producer = kafka . producer (); await producer . connect (); // Send a single message await producer . send ({ topic : " scraped-products " , messages : [ { key : " product-123 " , value : JSON . stringify ({ title : " Widget " , price : 29.99 , url : " https://... " }), headers : { source : " amazon " , scrapedAt : Date . now (). toString () }, }, ], }); // Batch send await producer . sendBatch ({ topicMessages : [ { topic : " scraped-products " , messages : products . map ( p => ({ key : p . id , value : JSON . stringify ( p ) })), }, { topic : " scrape-metrics " , messages : [{ value : JSON . stringify ({ total : products . length , duration : elapsed }) }], }, ], }); Consumer: Process Events const consum
Continue reading on Dev.to JavaScript
Opens in a new tab


