Search code examples
javarestweb-serviceswebservice-client

How to make a server API tell a client API that an update is available


I have rest client API accessing data through /read endpoint from my server. However, I have an internal batch service that calls my server's /write endpoint which updates the data. So from time to time the data from the server gets updated. How will I tell the client that the data is updated and ask it to call the /read endpoint again to get the latest data?

Below is a highlevel diagram on the scenario.

enter image description here


Solution

  • Try learning webhooks? Basically, the client will "subscribe" to your webhook, or to make it easier, the client will provide an endpoint to the server. Whenever there's an update, your server just sends a request to the client. The client should simply call a service that, whenever it receives a request from the webhook, fetches read.

    It basically goes like this

    • Client subscribes to server
    • The client gives the server an endpoint /updateAvailable
    • When there's an update, the server sends a request to client's endpoint /updateAvailable
    • '/updateAvailable' invokes a service that calls on '/read'

    The '/updateAvailable' endpoint could invoke a service that updates content. Let's say the request sent has a parameter

    {
       "updateFound":true
    }
    

    So whenever the client's '/updateAvailable' is called and receives a request, you do something like this (pseudo code)

    if (updateBody.updateFound.message=true)
    then call read()
    

    Edit

    By creating an endpoint for client, you can also do automatic updates. So client has an /updateAvailable endpoint. The server sends the update to the /updateAvailable endpoint, which from the client side invokes whatever service is used for /read