← Back to blog
Engineering

Building Real-Time Data Pipelines with TypeScript and Apache Kafka

Scalable data pipelines with Kafka

F

Fulcra Team

9 May 2026 · 4 min read

Building Real-Time Data Pipelines with TypeScript and Apache Kafka

Introduction

Building real-time data pipelines is crucial for many applications, especially in the Fintech industry where data needs to be processed quickly and efficiently. Apache Kafka is a popular choice for building such pipelines due to its scalability and fault-tolerance. In this post, we will explore how to build a real-time data pipeline using TypeScript and Apache Kafka.

Why Apache Kafka

Apache Kafka is a distributed streaming platform that is designed to handle high-throughput and provides low-latency, fault-tolerant, and scalable data processing. It is widely used in many industries for building real-time data pipelines. Kafka provides a number of benefits, including:

  • Scalability: Kafka can handle large amounts of data and scale horizontally by adding more brokers to the cluster.
  • Fault-tolerance: Kafka provides fault-tolerance by replicating data across multiple brokers, ensuring that data is not lost in case of a failure.
  • Low-latency: Kafka provides low-latency data processing, making it suitable for real-time applications.

Building a Real-Time Data Pipeline

To build a real-time data pipeline using TypeScript and Apache Kafka, we need to follow these steps:

Step 1: Install Dependencies

First, we need to install the required dependencies, including @types/kafka and kafkajs.

npm install --save kafkajs
npm install --save-dev @types/kafka

Step 2: Create a Kafka Producer

Next, we need to create a Kafka producer that will send data to the Kafka topic.

import { Kafka } from 'kafkajs';

const kafka = new Kafka({
  clientId: 'my-app',
  brokers: ['localhost:9092'],
});

const producer = kafka.producer();

async function sendData(data: string) {
  await producer.send({
    topic: 'my-topic',
    messages: [data],
  });
}

Step 3: Create a Kafka Consumer

Then, we need to create a Kafka consumer that will subscribe to the Kafka topic and process the data.

import { Kafka } from 'kafkajs';

const kafka = new Kafka({
  clientId: 'my-app',
  brokers: ['localhost:9092'],
});

const consumer = kafka.consumer({ groupId: 'my-group' });

async function startConsumer() {
  await consumer.subscribe({ topic: 'my-topic' });
  await consumer.run({
    eachMessage: async ({ topic, partition, message }) => {
      console.log(`Received message: ${message.value.toString()}`);
      // Process the message
    },
  });
}

Example Use Case

Let's consider an example use case where we need to build a real-time data pipeline to process stock prices. We can use Apache Kafka to build a pipeline that can handle high-throughput and provides low-latency data processing.

Step 1: Create a Kafka Topic

First, we need to create a Kafka topic that will store the stock prices.

kafka-topics --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 my-topic

Step 2: Send Stock Prices to Kafka

Next, we need to send the stock prices to the Kafka topic using the Kafka producer.

import { Kafka } from 'kafkajs';

const kafka = new Kafka({
  clientId: 'my-app',
  brokers: ['localhost:9092'],
});

const producer = kafka.producer();

async function sendStockPrice(stockPrice: number) {
  await producer.send({
    topic: 'my-topic',
    messages: [JSON.stringify({ stockPrice })],
  });
}

Step 3: Process Stock Prices

Then, we need to process the stock prices using the Kafka consumer.

import { Kafka } from 'kafkajs';

const kafka = new Kafka({
  clientId: 'my-app',
  brokers: ['localhost:9092'],
});

const consumer = kafka.consumer({ groupId: 'my-group' });

async function startConsumer() {
  await consumer.subscribe({ topic: 'my-topic' });
  await consumer.run({
    eachMessage: async ({ topic, partition, message }) => {
      const stockPrice = JSON.parse(message.value.toString()).stockPrice;
      console.log(`Received stock price: ${stockPrice}`);
      // Process the stock price
    },
  });
}

Conclusion

In this post, we explored how to build a real-time data pipeline using TypeScript and Apache Kafka. We discussed the benefits of using Kafka and provided a step-by-step guide on how to build a pipeline. We also provided an example use case where we built a pipeline to process stock prices. If you're interested in learning more about building scalable and fault-tolerant systems, contact us to discuss how we can help you achieve your goals.

Share