DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Event-Driven Pipelines With Apache Pulsar and Go
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • Kafka and Spark Structured Streaming in Enterprise: The Patterns That Hold Up Under Pressure
  • Exactly-Once Processing: Myth vs Reality

Trending

  • From APIs to Actions: Rethinking Back-End Design for Agents
  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • Product-Led Software Delivery: Intelligent Platforms for DevOps at Scale
  • Docker Hardened Images Are Free Now — Here's What You Still Need to Build
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Building a Simple Kafka Client for the Web and Desktop

Building a Simple Kafka Client for the Web and Desktop

This article is about building a simple Kafka real-time web client using KafkaJS, Socket.io, and ElectronJS.

By 
Danesh Hussain Zaki user avatar
Danesh Hussain Zaki
·
Nov. 03, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
13.0K Views

Join the DZone community and get the full member experience.

Join For Free

Kafka is an open-source platform that is used for event distribution, logging, and streaming. Distributions from Confluent and other vendors provide a web UI for broker management and client tasks, while those from Apache Kafka provide just command-line tools. Also, for most of the requirements, a simple client that publishes and consumes events or messages from the topic is sufficient. The main objectives of building such a client were:

  • Follow a web development approach 
  • Have a simple and elegant UI 
  • Build a native desktop app

Kafka client UI

Being a sort of a de facto standard for most web development with a huge selection of frameworks and libraries, node.js was chosen as the platform. The UI framework was Bootstrap with a Material theme. Though some might argue that it is dated and has lesser capabilities, it fit the bill for the requirement in question. ElectronJS was used to provide the native desktop app capabilities. It has a flat learning curve with usually only a main file that it generates with functionality to manage the window being displayed. The Kafka client-specific code can be in separate files and can be included in this file. Using utilities such as electron packager, the executable code for the respective platform can be generated.

JavaScript
 




x
54


 
1
/**
2
 * This file creates and manages the Electron JS window
3
 */
4

          
5
const { app, BrowserWindow } = require('electron')
6

          
7
function createWindow () {
8
  // Create the browser window.
9
  const win = new BrowserWindow({
10
    width: 1250,
11
    height: 600,
12
    icon: __dirname + '/images/icon.ico',
13
    autoHideMenuBar: true,
14
    resizable: true,
15
    fullscreenable:false, 
16

          
17
    webPreferences: {
18
      nodeIntegration: true,
19
      devTools : false
20
    }
21
  })
22

          
23
  //no menu 
24
  win.removeMenu();
25
  
26
  // and load the index.html of the app.
27
  win.loadFile('index.html')
28

          
29
}
30

          
31
// This method will be called when Electron has finished
32
// initialization and is ready to create browser windows.
33
// Some APIs can only be used after this event occurs.
34
app.whenReady().then(createWindow)
35

          
36
// Quit when all windows are closed, except on macOS. There, it's common
37
// for applications and their menu bar to stay active until the user quits
38
// explicitly with Cmd + Q.
39
app.on('window-all-closed', () => {
40
  if (process.platform !== 'darwin') {
41
    app.quit()
42
  }
43
})
44

          
45
app.on('activate', () => {
46
  // On macOS it's common to re-create a window in the app when the
47
  // dock icon is clicked and there are no other windows open.
48
  if (BrowserWindow.getAllWindows().length === 0) {
49
    createWindow()
50
  }
51
})
52

          
53
// call the kokpitsever class to handle the Kafka client functionality
54
const kokpitserver = require("./kokpitserver");



For the Kafka client functionality, KafkaJS was used as the library as it is lightweight and provides a simple interface. The features built using KafkaJS were:

  • Connecting automatically to a Kafka broker
  • Creating a topic on the fly
  • Publishing and consuming messages 

kafka topic

To provide a real-time view of the messages in the Kafka topic, Socket.io is used. It enables real-time communication using WebSockets from the Kafka topic to the UI. The consumer sees the messages on the topic it has subscribed to in the UI in real-time as they get published. The code to implement comprises adding a line of code to emit the event at the server and to consume it in the UI and display it.

JavaScript
 




xxxxxxxxxx
1


 
1
//server side
2
await consumer.run({
3
    eachMessage: async ({ topic, partition, message }) => {
4
        io.sockets.emit('message', message.value.toString())
5
    },
6
});



JavaScript
 




x


 
1
//script in HTML
2
var socket = io('http://localhost:8182');
3

          
4
    //on message 
5
    socket.on('message', function (msg) {
6
        alert("Message: " + msg);
7
    });



Adding new features to the Kafka client or making it available on more platforms is not as difficult as the underlying libraries and frameworks are robust and well adopted. This approach for building a real-time web client be used for messaging platforms such as RabbitMQ, Solace, and others. 

The code for the client is available here. The latest release is available here.

kafka Desktop (word processor)

Opinions expressed by DZone contributors are their own.

Related

  • Event-Driven Pipelines With Apache Pulsar and Go
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • Kafka and Spark Structured Streaming in Enterprise: The Patterns That Hold Up Under Pressure
  • Exactly-Once Processing: Myth vs Reality

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook