<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Modak Verma on Medium]]></title>
        <description><![CDATA[Stories by Modak Verma on Medium]]></description>
        <link>https://medium.com/@modakverma02?source=rss-8dac094f78ce------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fl4bHis4PKX8jeVru81Lig.jpeg</url>
            <title>Stories by Modak Verma on Medium</title>
            <link>https://medium.com/@modakverma02?source=rss-8dac094f78ce------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 17:19:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@modakverma02/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Streamlining Real-Time Data Processing: Apache Kafka with NodeJs]]></title>
            <link>https://medium.com/@modakverma02/streamlining-real-time-data-processing-apache-kafka-with-nodejs-54e9deed201b?source=rss-8dac094f78ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/54e9deed201b</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[development-tools]]></category>
            <category><![CDATA[integration]]></category>
            <category><![CDATA[realtime]]></category>
            <dc:creator><![CDATA[Modak Verma]]></dc:creator>
            <pubDate>Sun, 15 Oct 2023 18:43:34 GMT</pubDate>
            <atom:updated>2023-10-15T19:16:14.752Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Apache Kafka, developed by Apache Software Foundation, is a distributed event store and stream-processing platform.</blockquote><h4>Problem Statement:</h4><p>Consider a scenario where you need to design a <a href="https://hazelcast.com/glossary/real-time-stream-processing/">real-time</a> food delivery monitoring system…</p><p>Here, the delivery agent’s location is updated at regular intervals, such as every second, we encounter a critical challenge i.e the concept of <a href="https://docs.oracle.com/cd/E17276_01/html/programmer_reference/transapp_throughput.html"><strong>Throughput</strong></a><strong> , </strong>in databases. Most databases today struggle with low throughput, making them ill-equipped to handle the influx of real-time data, potentially leading to system instability. As a result our database can not handle this huge volume of data, and is likely to go down…👎</p><p>Throughput, often measured as the <em>number of transactions a database can process per second</em>, becomes a crucial consideration in ensuring the smooth functioning of such systems. In this article, we’ll embark on a journey to explore the seamless integration of <a href="https://kafka.apache.org/">Kafka </a>with <a href="https://nodejs.org/en/docs">Node.js</a> . By doing so, we’ll open the door to a world of possibilities, empowering you to develop applications capable of efficiently managing high volumes of real-time data. Whether you’re, an experienced developer or just beginning your journey into event-driven architecture, this guide will provide valuable insights and practical steps for harnessing Kafka’s capabilities within the Node.js ecosystem.</p><p>But before we dive into the nitty-gritty of how to integrate these two powerful tools, let’s take a moment to understand why Kafka and Node.js are a match made in digital heaven, and what benefits this tool can bring to your Node.js projects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6aDj8f4afZQHvlLS2DAJAw.png" /><figcaption><strong>“ SuperHero Kafka </strong>💪<strong> ”</strong></figcaption></figure><h3><strong>Powers of Kafka </strong>⚡</h3><ul><li><strong>Scalability:</strong> Kafka is designed to handle massive amounts of data and is horizontally scalable.</li><li><strong>Real-Time Data Streaming:</strong> Kafka enables real-time data streaming, allowing data to be processed as it is generated.</li><li><strong>High Throughput:</strong> Kafka can handle a high throughput of messages, making it ideal for applications that require fast data ingestion and processing.</li><li><strong>Low Latency:</strong> Kafka is designed to minimize message delivery latency. It ensures that messages are delivered to consumers with minimal delay.</li></ul><h3>*** Integrating Kafka with NodeJs🔥</h3><p><strong><em>Step 1:<br></em></strong><em>Start </em><a href="https://zookeeper.apache.org/"><em>Zookeeper</em></a><em> Container and expose PORT</em> 2181:</p><pre>docker run -p 2181:2181 zookeeper</pre><p><em>→ “ ZooKeeper is a distributed coordination and synchronization service that is widely used in distributed systems and applications.It primarily manages and coordinates Kafka brokers . ”</em></p><p><em>Start Kafka Container, expose PORT </em><em>9092 and setup ENV variables.</em></p><pre>docker run -p 9092:9092 \<br>-e KAFKA_ZOOKEEPER_CONNECT=&lt;PRIVATE_IP&gt;:2181 \<br>-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://&lt;PRIVATE_IP&gt;:9092 \<br>-e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \<br>confluentinc/cp-kafka</pre><p>-&gt; replace your &lt;PRIVATE_IP&gt; with your IP_ADDRESS, and run the Kafka container.</p><p><strong>Step 2:(*Backend)</strong></p><blockquote>- Open VSCode and create a node backend by “npm init”</blockquote><ul><li>Install package ‘Kafkajs’: “npm install kafkajs”</li><li>Create “client.js” file:</li></ul><pre>const {Kafka} = require(&quot;kafkajs&quot;);<br><br>exports.kafka = new Kafka({<br>    clientId:&quot;my-app&quot;,<br>    brokers: [&#39;localhost:9092&#39;]<br>});</pre><blockquote>- Create “admin.js” file:<br><em>Create a ‘</em> kafkaTopic’ <em>by</em> <em>defining ‘</em>topic’ <em>&amp;</em> <em>‘</em>numPartitions’.</blockquote><pre>const {kafka} = require(&#39;./client&#39;)<br><br>async function init(){<br>    const admin = kafka.admin();<br>    console.log(&quot;Admin Connecting...&quot;);<br>    await admin.connect();<br>    console.log(&quot;Admin Connection Success...&quot;);<br>    console.log(&#39;creating topic [rider-updates]&#39;)<br>    await admin.createTopics({<br>       topics: [{<br>           topic: &#39;rider-updates&#39;,<br>           numPartitions: 2,<br>       }]<br>    })<br>    console.log(&quot;topic created success [rider-updates]&quot;);<br>    console.log(&quot;Disconnecting Admin...&quot;);<br>    await admin.disconnect();<br>}<br>init();</pre><blockquote>- Create “producer” file:<br><em>Create a post route ‘/produce’ with ‘express.Router()’ &amp; destructure </em>name<em> &amp; </em>loc <em>parameters from ‘req.body’.<br>Now, create a “</em>kafka producer variable”<em> and </em>“connect” <em>it as producer.connect().<br>Define </em>topic<em> and a </em>partitioning condition <em>inside it</em>.</blockquote><pre>const {kafka} = require(&#39;./client&#39;);<br>const express = require(&quot;express&quot;);<br>const router = express.Router();<br><br>router.post(&#39;/produce&#39;,async(req,res)=&gt;{<br>const {name,loc} = req.body;<br>    const producer = kafka.producer();<br>    console.log(&#39;Connecting Producer...&#39;);<br>    await producer.connect();<br>    console.log(&quot;producer connected successfully...&quot;);<br><br>       await producer.send({<br>        topic: &#39;rider-updates&#39;,<br>        messages:[<br>            {<br>                partition: loc.toLowerCase() === &quot;north&quot;?0:1,<br>                key: &#39;location-update&#39;,<br>                value: JSON.stringify({<br>                    name:name,<br>                    loc:loc<br>                })<br>            }<br>        ]<br>    })<br>    res.status(200).send({<br>        messages:&quot;successs&quot;<br>    })<br>})<br>module.exports = router;</pre><blockquote>- Create “consumer” file.<br><em>Declare a </em>latestMessage<em> variable which will store our latest produced messages.<br>You can set up your </em>clientId<em> as anything. <br>Setup your brokers as: </em>&lt;YOUR_IP_ADDRESS&gt;/localhost : 9092 ‘i.e Kafka listening port’.</blockquote><blockquote><em>Define a consumer</em> groupId <em>as</em> ‘group-1’.<br>* Connect <em>consumer</em> <em>&amp;</em> Subscribe <em>it</em> <em>to the defined</em> topic.<br><em>Run the Kafka consumer which will modify our </em>latestMessage <em>variable with the produced message value.<br>Create a get route ‘ / ’ and send the ‘</em>latestMessage’ <em>over it</em>.</blockquote><pre>const express = require(&quot;express&quot;);<br>const { Kafka } = require(&#39;kafkajs&#39;); <br>const cors = require(&quot;cors&quot;);<br><br>const app = express();<br>app.use(express.json());<br><br>app.use(cors({<br>    credentials: true,<br>    origin: &#39;http://localhost:3000&#39;,<br>}));<br><br>let latestMessage = &#39;&#39;;<br>async function init() {<br>    const kafka = new Kafka({<br>        clientId: &#39;my-app&#39;,<br>        brokers: [&#39;localhost:9092&#39;]<br>    });<br>    <br>    const consumer = kafka.consumer({ groupId: &#39;group-1&#39; });<br><br>    await consumer.connect();<br>    await consumer.subscribe({<br>        topic: &quot;rider-updates&quot;,<br>        fromBeginning: true<br>    });<br><br>    await consumer.run({<br>        eachMessage: async ({ topic, partition, message }) =&gt; {<br>            console.log(`[${topic}]: PART:${partition}:`, message.value.toString());<br>            latestMessage = message.value.toString(); <br>        }<br>    });<br>}<br><br>const produceRoute = require(&#39;./producer&#39;); <br>app.use(&#39;/api&#39;, produceRoute);<br><br>app.get(&#39;/&#39;, (req, res) =&gt; {<br>    res.status(200).send(latestMessage);<br>});<br><br>app.listen(4000, () =&gt; {<br>    console.log(`Express app is listening on port 4000`);<br>    init();<br>}); </pre><blockquote>Start the backend server: &gt; node consumer.js</blockquote><blockquote><strong><em>Step 3: (Frontend)<br>- </em></strong><em>Created a </em><a href="https://react.dev/learn"><em>React</em></a><em> frontend in the root directory and create pages to handle the two backend (GET &amp; POST) routes… </em>🚀</blockquote><blockquote><strong><em>**Entire Code GitHub Link: </em></strong><a href="https://github.com/modakverma/kafka-nodejs">https://github.com/modakverma/kafka-nodejs</a></blockquote><h3><strong>Final Result Video Demonstration:</strong></h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FoKjBaW5AvVA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DoKjBaW5AvVA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FoKjBaW5AvVA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7321c1a23c86322f86d2ac6238b52d9e/href">https://medium.com/media/7321c1a23c86322f86d2ac6238b52d9e/href</a></iframe><p>Booyah! 🤠 <br>Hope you learned something new …</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54e9deed201b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>