'How REST API handle continuous data update

I have REST backend api, and front end will call api to get data.

I was wondering how REST api handles continuous data update, for example, in jenkins, we will see that if we execute build job, we can see the continous log output on page until job finishes. How REST accomplish that?



Solution 1:[1]

Jenkins will just continue to send data. That's it. It simply carries on sending (at least that's what I'd presume it does). Normally the response contains a header field indicating how much data the response contains (Content-Length). But this field is not necessary. The server can omit it. In such a case the response body ends when the server closes the connection. See RFC 7230:

Otherwise, this is a response message without a declared message body length, so the message body length is determined by the number of octets received prior to the server closing the connection.

Another possibility would be to use the chunked transfer encoding. Then the server sends a chunk of data having its own Content-Length header. The server terminates this by sending a zero-length last chunk.

Websocksts would be a third possibility.

Solution 2:[2]

I was searching for an answer myself and then the obvious solution struck me. In order to see what type of communication a service is using, you can simply view it from browser side using Developer Tools.

In Google Chrome it will be F12 -> Network.

In case of Jenkins, front-end is sending AJAX requests to backend for data:

  • every 5 seconds on Dashboards page
  • every second during Pipeline run (Console Output page), that you have mentioned.

I have also checked the approach in AWS. When checking the status of instances (example: Initializing... , Booting...), it queries the backend every second. It seems to be a standard interval for its services.

Additional note:

When running an AWS Remote Console though, it first sends requests for remote console instance status (backend answers with { status: "BOOTING" }, etc.). After backend returns status as "RUNNING", it starts a WebSocket session between your browser and AWS backend (you can notice it by applying WS filter in developer tools). Then it is no longer REST API, but WebSockets, that is a different protocol (stateful).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 jakjus