Home > Net >  How to continues send data from backend to frontend when something changes
How to continues send data from backend to frontend when something changes

Time:03-08

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?

CodePudding user response:

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

CodePudding user response:

Use WebSockets

socket.on('change' callback())

Hope, it helps.

  • Related