let's say I have the following Java code.
get("/", (request, response) -> {
Map<String, Object> attributes = new HashMap<>();
//attributes.put("message", "Hello World!");
return new ModelAndView(attributes, "index.ftl");
}, new FreeMarkerEngine());
That is from Spark. When I navigate to localhost:portnumber/, I see index.ftl rendered, which let's assume (not coded here) displays data from a database for this app. But let's say I wanted to dynamically update index.ftl. Let's say another user updated the database (not coded here) from another instance of the application, and I wanted to display the new changes in index.ftl on the first user's page. How would this be done without having to re-render the pages?
You can't just simply have a timer in the Java side which pulls in the new data every 10-20 milliseconds. That would be a massive waste of connection time as well. Can the Java code be pinged somehow that the database has been updated? Like a listener for the database?
Not only is that a problem, but how would you be able to push the newly received data to index.ftl without having to rerender?
There are several different COMET techniques that you could use to achieve the effect. I have found a very comprehensive article is useful to compare and choose between them.
After employing COMET, you could utilize this trick so that you only refresh the contents within a <div>
tag
Another reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With