Search code examples
node.jstimeserverserver-sideclient-side

Update client-side information from server, without sending a request each time


I am currently developing my own desktop clock app and after successfully receiving the current date and time via custom API and locally, I've come to the point where serious complications may occur in the future.

With the current implementation of (local-time), the time is updated locally - every minute per app instance.

It would be unnecessary silly if I try to achieve the same for the (server-time) -> to send a GET request each minute to the Server from every existing app instance...

So, here comes my question..

What are the more efficient alternatives?

P.S. The server environment is Node.js. The received time is in the form of a JSON.


Solution

  • you can try this code

    const serverTime = new Date(); // lets say this is time from server
    
    const timer = (time) =>{
      setTimeout(()=>{
          time.setSeconds(time.getSeconds() + 1);
          console.log(time)
          timer(time);
      },1000)
    }
    timer(serverTime);