6

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?

darijan
  • 9,725
  • 25
  • 38
user41912
  • 557
  • 1
  • 6
  • 18
  • Do you mean something like [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events)? – UnholySheep Oct 15 '16 at 19:27
  • Or alternatively [Websockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) (as per [this tutorial](https://sparktutorials.github.io/2015/11/08/spark-websocket-chat.html)) – UnholySheep Oct 15 '16 at 19:31
  • @UnholySheep That is exactly what I mean, but for a Java backend instead. In Javascript, AJAX is the way to go. How would I make server-sent requests in the Spark framework? – user41912 Oct 15 '16 at 19:31
  • @UnholySheep I see. That tutorial I did come across some time ago. Is WebSocket technology considered outdated now? Generally speaking, of course? Is it considered bad practice? Especially since everything is AJAX nowadays? – user41912 Oct 15 '16 at 19:32
  • I'm not an expert on current trends, but AJAX, Websockets and SSE have completely different usages IMO. AJAX is for everything initiated only from the client (and often combined with RESTful services), SSE is if only the server needs to notify the client and Websockets is for ("real-time") communication in both ways. I don't think any of them should be considered outdated it all depends on your use-case – UnholySheep Oct 15 '16 at 19:36
  • @UnholySheep Interesting. As someone who is a college student and just got into this, I too don't know much about current trends or differences between the two. I simply assumed that AJAX, SSE, and WebSockets all had similar uses. I think overall, they do allow for "updating with page reload" in some sense, at least. Would you agree? – user41912 Oct 15 '16 at 20:02
  • Yes, in a (very) broad sense that is what all 3 try to achieve. However, as I mentioned in my previous comment, the way they do it differs (especially *who* initiates the change/update) – UnholySheep Oct 15 '16 at 20:04

1 Answers1

1

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

Community
  • 1
  • 1
akgren_soar
  • 337
  • 4
  • 9