Apache Kafka
Data Streaming
Web Development
Real-Time Data
Data Transmission

Sending Apache Kafka data on web page

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a powerful distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. As web technologies have evolved, the need for real-time data streaming and processing has become crucial. Integrating Apache Kafka with web applications allows developers to send data directly to a webpage in real-time, enhancing the interactivity and user experience.

Understanding Apache Kafka Architecture

To understand how to send data from Apache Kafka to a web page, it's crucial to grasp the basic components of Kafka:

  • Producer: Responsible for publishing records to Kafka topics.
  • Broker: A server in the Kafka cluster that stores data and serves clients.
  • Topic: A category or feed name to which records are published.
  • Consumer: Consumes records from one or more Kafka topics.

Setting Up Apache Kafka

Before integrating Kafka with a web application, set up a Kafka broker. You can use platforms like Confluent Cloud or install Kafka on your server. For local development, setting up Kafka using Docker is a sensible choice.

bash
# Run Kafka using Docker
docker run -p 2181:2181 -p 9092:9092 --env ADVERTISED_HOST=localhost --env ADVERTISED_PORT=9092 spotify/kafka

Sending Data to a Web Page

The high-level process of sending data from Kafka to a web page involves:

  1. Producing Data: Data is produced to a Kafka topic by backend systems.
  2. Consuming Data: A backend component subscribes to the topic and fetches the data.
  3. Sending to Frontend: The backend uses WebSockets or Server-Sent Events (SSE) to push data to the frontend.
  4. Displaying Data: The frontend updates the web page in real-time as data is received.

Utilizing WebSockets with Kafka

WebSockets provide a full-duplex communication channel over a single, long-lived connection. By using WebSockets, you can establish a persistent connection between the client (web page) and server.

Backend Setup

On the server side, you might choose Node.js with the kafka-node library to consume data and ws for WebSocket communication:

javascript
1const { KafkaClient, Consumer } = require('kafka-node');
2const WebSocket = require('ws');
3const wss = new WebSocket.Server({ port: 8080 });
4
5const client = new KafkaClient({ kafkaHost: 'localhost:9092' });
6const consumer = new Consumer(
7    client,
8    [{ topic: 'web-updates', partition: 0 }],
9    { autoCommit: false }
10);
11
12wss.on('connection', ws => {
13    consumer.on('message', function (message) {
14        ws.send(JSON.stringify(message));
15    });
16});

Frontend Setup

In the web page, establish a WebSocket connection to receive messages:

javascript
1const socket = new WebSocket('ws://localhost:8080');
2
3socket.onmessage = function (event) {
4    const data = JSON.parse(event.data);
5    console.log('Data received:', data);
6    // Update the web page DOM based on received data
7};

Using Server-Sent Events (SSE)

Server-Sent Events are a simpler alternative for one-way communication from the server to the client. Unlike WebSockets, SSE can be implemented using standard HTTP connections.

Backend Implementation

With Node.js, you can set up an SSE endpoint using Express:

javascript
1const express = require('express');
2const { KafkaClient, Consumer } = require('kafka-node');
3
4const app = express();
5const client = new KafkaClient({ kafkaHost: 'localhost:9092' });
6const consumer = new Consumer(
7    client,
8    [{ topic: 'web-updates', partition: 0 }],
9    { autoCommit: false }
10);
11
12app.get('/events', (req, res) => {
13    res.writeHead(200, {
14        'Content-Type': 'text/event-stream',
15        'Cache-Control': 'no-cache',
16        'Connection': 'keep-alive',
17    });
18
19    consumer.on('message', function (message) {
20        res.write(`data: ${JSON.stringify(message)}\n\n`);
21    });
22});
23
24app.listen(3000, () => console.log('SSE server running on port 3000'));

Frontend Implementation

Connect to the SSE endpoint from the web page:

javascript
1const eventSource = new EventSource('/events');
2
3eventSource.onmessage = function(event) {
4    const data = JSON.parse(event.data);
5    console.log('Data received:', data);
6    // Update DOM accordingly
7};

Summary

Here’s a summary of key points covered:

FeatureWebSocketsSSE
ConnectionFull duplexSingle direction (server to client)
ComplexityModerateSimple
Use CaseReal-time bi-directional communicationReal-time one-way communication
Browser SupportBroad, except old browsersBroad, except IE

Conclusion

Integrating Apache Kafka with web applications using WebSockets or SSE enables real-time data updates on web pages. This capability is essential for applications requiring live data feeds, such as financial tickers, social media updates, or IoT monitoring systems. By choosing the right technology based on the application's needs, developers can implement efficient, real-time data streams directly into their web interfaces.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.