Member-only story
Spring Boot | Nginx | WebSocket | RabbitMQ | Apache Kafka | PostgreSQL
Implementing an Interactive and Scalable News Broadcasting App
Combining Nginx, WebSockets, and message brokers for instant news updates
In this article, we will show how to implement an interactive news broadcasting Spring Boot application called News App. A user can post news using a REST API, and it gets sent out to users instantly through a live WebSocket connection. Users can react by liking or disliking the news, and the app keeps track of all those reactions to show the total likes and dislikes.
News App
News App is a Spring Boot Java web app that provides a REST endpoint for publishing and broadcasting news. It also supports real-time broadcasting and user reactions through full-duplex WebSocket channels.
The app stores data in PostgreSQL and runs behind a Nginx load balancer. For broadcasting, it connects to RabbitMQ, and user reactions are handled sequentially and partitioned by news ID using an Apache Kafka topic partition.
Project Diagram
Flow of Events
Consider:
- All three news-app instances run behind a Nginx load balancer.
- Browser-1 is connected via a full-duplex WebSocket channel to news-app-1, Browser-2 to news-app-2, and Browser-3 to news-app-3.
- 1.1. Users publishes a news by calling the REST API
POST /api/news -d { "description": "..." }, through the Nginx load balancer URL. - 1.2. Nginx forwards the requests to one of the news-app instances, in the example, news-app-1.
- 1.3. The news-app-1 processes the request and saves the news in the PostgreSQL database.
- 1.4. The news-app-1 sends the news event to RabbitMQ (fanout exchange).
- 2.a, 2.b, 2.c. All new-app instances receive the news event from RabbitMQ.
- 3.a, 3.b, 3.c. All new-app instances broadcast the news event to all connected clients through WebSocket channels via Nginx load balancer.
- 4.a, 4.b, 4.c. Users react to the news by sending “like” or “dislike” reactions through WebSocket channels.
- 5.a, 5.b, 5.c. The news-app instances that receives the reactions sends them to an Apache Kafka topic partitioned by news id.
- 6.a, 6.b, 6.c. The news-app handling the news id partition, in this case news-app-2, processes the reactions sequentially and updates the news statistics in the PostgreSQL database.
- 7.1, 7.2, 7.3. Users can check the news statistics through the REST API
GET /api/news.
Demo
- We opened three browsers and one terminal.
- In the terminal, we ran the curl command
curl -X POST http://news-app.lb/api/newsto publish a news. This creates a random news entry. - The news appears simultaneously in the browsers we opened earlier.
- Next, we liked the news in all the browsers.
- Finally, we checked the news statistics by running
curl http://news-app.lb/api/news/{id}in the terminal. It correctly shows 3 likes for the news.
Source Code
You can find the complete code, along with instructions on how to run it on your local machine, in the GitHub repository ivangfr/spring-boot-nginx-websocket-rabbitmq-kafka.
Support and Engagement
If you enjoyed this article and would like to show your support, please consider taking the following actions:
- 👏 Engage by clapping, highlighting, and replying to my story. I’ll be happy to answer any of your questions;
- 🌐 Share my story on Social Media;
- 🔔 Follow me on: Medium | LinkedIn | X | GitHub;
- ✉️ Subscribe to my newsletter, so you don’t miss out on my latest posts.







