Saturday, October 4, 2025

Implementing Real-Time Admin Notifications with Kafka in Spring Boot Microservices

 

Introduction

In modern microservices architectures, real-time event-driven communication is essential for building responsive applications. This post walks through implementing Kafka-based admin notifications across two independent Spring Boot microservices: a Todo App (handling user management) and a Blog service (handling content and comments).

Architecture Overview

Our setup consists of:

  • Todo App Microservice (Port: 8081) - Manages users and authentication
  • Blog Microservice (Port: 8083) - Manages blog posts and comments
  • Kafka - Message broker for inter-service communication
  • WebSocket - Real-time push notifications to frontend
  • React Frontend - Displays toast and bell notifications

Both services run independently but communicate through Kafka topics to deliver real-time notifications to administrators.

Use Cases

  1. Todo App: Admin receives notification when any user logs in
  2. Blog Service: Admin receives notification when any user posts a comment (since comments require moderation)

Implementation Steps

Step 1: Create BlogProducer.java

The producer is responsible for publishing events to Kafka when significant actions occur in your application.

Step 2: Create BlogConsumer.java

The consumer listens to Kafka messages and forwards them to WebSocket clients in real-time.

Step 3: Create WebSocketConfig.java

Configure WebSocket endpoints to enable real-time communication with the frontend.

Step 4: Update CommentController/Service

Integrate the producer into your existing comment creation logic.

Step 5: Update Frontend - AdminNotifications.js

Connect to the WebSocket endpoint and display notifications.

Step 6: Required Dependencies

Add  dependencies to your pom.xml.

Add to configure application.properties.

The Complete Flow

  1. User posts a comment on a blog post
  2. CommentController saves the comment and calls BlogProducer
  3. BlogProducer publishes message to Kafka topic blog-notifications
  4. BlogConsumer (listening to Kafka) receives the message
  5. BlogConsumer forwards message to WebSocket endpoint /topic/admin-notifications
  6. Frontend (connected via WebSocket) receives the notification
  7. Toast notification appears on screen
  8. Bell icon updates with unread count

Testing Your Implementation

  1. Start Kafka and Zookeeper
  2. Start both microservices (Todo App and Blog)
  3. Login as an admin user
  4. Open browser console to verify WebSocket connection
  5. Post a comment (either as admin or regular user)
  6. Verify toast notification appears
  7. Verify bell icon shows unread count

Benefits of This Architecture

  • Decoupled Services: Todo and Blog services remain independent
  • Scalability: Kafka handles high message throughput
  • Real-time Updates: WebSocket provides instant notifications
  • Event-Driven: Easy to add more notification types
  • Fault Tolerance: Kafka persists messages if consumers are temporarily down

Extending the System

You can easily extend this pattern for additional notifications:

  • User registration events
  • Post publication notifications
  • Comment approval/rejection alerts
  • System-wide announcements

Simply create new Kafka topics and add corresponding producers/consumers for each event type.

Conclusion

By combining Kafka for inter-service communication and WebSocket for real-time frontend updates, we've built a robust notification system across independent microservices. This architecture scales well and provides the foundation for building more complex event-driven features in your application.

No comments:

Post a Comment

🧠 How to Upgrade Your Spring Boot Login for Full OWASP Protection (XSS, CSRF, HttpOnly JWT)

 Modern web apps often use localStorage for JWTs — but that’s risky. localStorage is accessible to JavaScript , so an XSS attack can easi...