0

I have a REST API made with Spring Boot. I can add entities (Persons) to a database using HTTP requests. I would like to have a web page display those entities dynamically: whenever a new Person is stored in the database, the page displays it on top of the list. No need to refresh the page.

What are the ways to achieve this? Would JSP be enough? Do I need javascript?

The simpler the better.

Mef
  • 13
  • 3

1 Answers1

1

What are the ways to achieve this? Would JSP be enough? Do I need javascript?

Yes you need javascript. But you can embed it in JSP too.

This is a solved problem, you will find numerous examples in the web if you search for "websocket spring". Example https://spring.io/guides/gs/messaging-stomp-websocket/

If you are not stuck in JVM-land and RDBMS, I would encourage you to take a look at firebase-angular stack (3-way binding)

Further read

Read this https://stackoverflow.com/a/12855533/6785908

Shameless copy since posting just a link is not advisable as link may break in future

In the examples below the client is the browser and the server is the webserver hosting the website.

Before you can understand these technologies, you have to understand classic HTTP web traffic first.

Regular HTTP:

  1. A client requests a webpage from a server.
  2. The server calculates the response
  3. The server sends the response to the client.

HTTP

Ajax Polling:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server at regular intervals (e.g. 0.5 seconds).
  3. The server calculates each response and sends it back, just like normal HTTP traffic.

Ajax Polling

Ajax Long-Polling:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server.
  3. The server does not immediately respond with the requested information but waits until there's new information available.
  4. When there's new information available, the server responds with the new information.
  5. The client receives the new information and immediately sends another request to the server, re-starting the process.

Ajax Long-Polling

HTML5 Server Sent Events (SSE) / EventSource:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which opens a connection to the server.
  3. The server sends an event to the client when there's new information available.

    • Real-time traffic from server to client, mostly that's what you'll need
    • You'll want to use a server that has an event loop
    • Not possible to connect with a server from another domain
    • If you want to read more, I found these very useful: (article), (article), (article), (tutorial).

HTML5 SSE

HTML5 Websockets:

  1. A client requests a webpage from a server using regular http (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which opens a connection with the server.
  3. The server and the client can now send each other messages when new data (on either side) is available.

    • Real-time traffic from the server to the client and from the client to the server
    • You'll want to use a server that has an event loop
    • With WebSockets it is possible to connect with a server from another domain.
    • It is also possible to use a third party hosted websocket server, for example Pusher or others. This way you'll only have to implement the client side, which is very easy!
    • If you want to read more, I found these very useful: (article), (article) (tutorial).

HTML5 WebSockets

Comet:

Comet is a collection of techniques prior to HTML5 which use streaming and long-polling to achieve real time applications. Read more on wikipedia or this article.

so-random-dude
  • 15,277
  • 10
  • 68
  • 113