Receiving Kafka event on web browser real time
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Browsers cannot talk to Kafka directly in the usual way because Kafka clients expect the Kafka wire protocol over TCP, and browsers do not expose that kind of raw socket access. The standard architecture is to consume Kafka on the server side and then push events to the browser through a web-friendly channel such as WebSocket or Server-Sent Events.
So the real answer is not “connect the browser to Kafka,” but “bridge Kafka to the browser through a backend service that handles protocol translation, authorization, and fan-out.”
The Usual Architecture
A common real-time path looks like this:
- Kafka producers write events to a topic.
- A backend consumer reads the topic.
- The backend forwards selected events to connected browsers.
- Browsers receive updates over WebSocket or SSE.
That backend layer is not optional. It is where you handle authentication, filtering, rate limiting, topic mapping, and replay policy.
WebSocket Bridge Example
A small Node.js example with kafkajs and ws illustrates the idea.
And the browser side is simple:
This is the basic pattern most real implementations follow.
WebSocket Versus SSE
WebSocket is a good fit when the browser also needs to send messages back to the server, such as acknowledgments, filters, or chat-style interaction.
If the flow is strictly server-to-browser, Server-Sent Events can be simpler.
Kafka events can then be written to each SSE client as text frames. The architecture is the same even though the browser transport changes.
What the Backend Should Do
The backend bridge should almost never forward raw Kafka topics to every browser client. Usually it should:
- authenticate the user
- decide which topic or tenant data that user may see
- transform the event into browser-safe JSON
- possibly buffer or debounce noisy event streams
This is where the real application design lives. Kafka is the transport backbone; the backend bridge is the policy and delivery layer.
Common Pitfalls
A common mistake is trying to expose Kafka brokers directly to browsers. Even ignoring protocol limitations, that is a bad security boundary.
Another issue is sending every Kafka event to every connected browser without filtering. That does not scale and quickly becomes a data-leak risk in multi-tenant systems.
Developers also sometimes forget that browser clients disconnect often. The bridge service should treat connections as ephemeral and tolerate reconnects naturally.
Finally, be explicit about replay behavior. Real-time dashboards often want only live updates, while notification UIs may need a recent event backlog as well.
Summary
- Browsers do not consume Kafka directly in the normal architecture.
- Use a backend consumer to bridge Kafka events into WebSocket or SSE.
- Put authentication, filtering, and fan-out logic in that backend layer.
- Choose WebSocket for bidirectional flows and SSE for simple server-to-browser streams.
- Design reconnect and replay behavior intentionally instead of treating live delivery as purely a transport problem.
Related reading
- Receiving Kafka Key in spring boot kafka listener
- Recommended settings for Kafka Internal Topics after upgrade to 1.0
- Reconnecting to Kafka with node-rdkafka is slow & inconsistent
- RecordTooLargeException in Kafka streams join
- Recommended way to embed PDF in HTML?
- Recursive Fetch All Items In DynamoDB Query using Node JS
- Redis / RabbitMQ - Pub / Sub - Performances
- Redis Pub/Sub vs Rabbit MQ

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.