Sitemap

ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Implementing an Interactive and Scalable News Broadcasting App

Combining Nginx, WebSockets, and message brokers for instant news updates

3 min read4 days ago

--

Press enter or click to view image in full size
Photo by ROBIN WORRALL on Unsplash

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

Press enter or click to view image in full size

Flow of Events

Press enter or click to view image in full size

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

Press enter or click to view image in full size
  1. We opened three browsers and one terminal.
  2. In the terminal, we ran the curl command curl -X POST http://news-app.lb/api/news to publish a news. This creates a random news entry.
  3. The news appears simultaneously in the browsers we opened earlier.
  4. Next, we liked the news in all the browsers.
  5. 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.

--

--

ITNEXT
ITNEXT

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Ivan Franchin
Ivan Franchin

Written by Ivan Franchin

Software Engineer and Open Source Contributor with BS and MS in CS. Writing about Java, Spring, software development in general, and emerging technologies.

No responses yet