Efaz
Efaz

Reputation: 304

Auto load data in the webpage when new data has been added in the database

I am creating a web application with Spring Boot and Java. Basically when a donor donates food, I want it to show in the restaurant homepage. Now the restaurant has to refresh their page to see the new item. How can I make the page auto-load whenever there is new data?

Upvotes: 1

Views: 664

Answers (2)

Mykhailo Moskura
Mykhailo Moskura

Reputation: 2211

You can to use sever side events.

In your case websocket would be overwork. Because from client you would not receive any calls etc . All that you need it’s only to notify UI about changes on backend. Its when requesting data from a server using simple XMLHttpRequest(AJAX). That’s why using SSE will be better choice.

Please look at this links:

https://www.baeldung.com/spring-server-sent-events

https://dzone.com/articles/spring-boot-server-sent-events-tutorial

Thanks

Upvotes: 1

Andreas
Andreas

Reputation: 159135

Assuming the database is only updated by the Spring web application, so you don't actually need the database to trigger the update event, you do this using Web Socket and STOMP.

See chapter 4. WebSockets in the Web on Servlet Stack part of the Spring Framework Documentation.

See "Using WebSocket to build an interactive web application" for a guide that walks you through the process of creating a “Hello, world” application that sends messages back and forth between a browser and a server.

Once you've learned how that works, you can broadcast a message to all interested clients (i.e. clients that are viewing the restaurant homepage) whenever a donor donates food for that restaurant (i.e. when the code updates the database).

Upvotes: 1

Related Questions