Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I continuously render an FTL page, lets say with updates?

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?

like image 350
user41912 Avatar asked Oct 15 '16 18:10

user41912


1 Answers

You can push data to browsers using COMET

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

like image 72
akgren_soar Avatar answered Sep 18 '22 14:09

akgren_soar