2

So I have a simple vanilla frontend, no frameworks because the site is so small. The site is a small webinterface so I can send dates to a database and load data to another database.

My coworker on the project have installed a bash script on another server, I have to run to start the loading data into a new database. Then the script writes to a file around every sixth second with an date I need to display on the frontend.

The backend is in java, and the frontend is pure html, css and vanilla js.

I have stumbled upon WatchService in java, which sounds like the thing I need. The problem is how do I send the data to the frontend, when it changes?

I could make a hack around it, with a setInterval in js, but isn't there a more natural/dynamic way?

2 Answers2

4

This is a major fundamental problem that has many different solutions with different architectures. The simplest one is polling where the client keeps sending requests to server with pre-set time intervals. The other is "Long Polling" - the idea is that client sends a request to the server but server doesn't reply until some event happens that client needs to be notified of - so the server just holds that request unitl it needs to use it to notify the client and then the client sends new request to the server and so forth. Another solution is "Push notifications" Yet another one is SSE - Server side events. So just search the web on the terms mentioned here: Polling, Long Polling, SSE Push Notifications. This is NOT a full list

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • The long polling is too old, at least for HTTP, you can use SSE instead. And WebSocket, RSocket(based on TCP/UDP, WebSocket, RCP protocol etc) are also great. – Hantsy Nov 13 '22 at 14:12
  • My answer relates to general principals. And also SSE is mentioned in the answer as well. And yes long polling is old but it is important for understanding general principle of the solution for this problem – Michael Gantman Nov 13 '22 at 14:18
1

Use WebSockets

socket.on('change' callback())

Hope, it helps.

Jas
  • 49
  • 2