2

We have a Rest Service that returns a text/event-stream from a POST endpoint, which contains a series of JSON Objects. (It is a Spring Boot / Kotlin RestController that returns a kotlinx.coroutines.flow.Flow<SomeJSONObject>) Now we want to consume this event-stream in a angular WebApplication, processing each object as it arrives. Unfortunately we don't know how this works. We tried the obvious things, like:

this.http.post(url, request)

or

this.http.post(url, request).toPromise().then(value => ...

and

this.http.post(url, request).subscribe(value => ...

It seems like the browser does not even make a request and does not receive any data. The backend service works fine, we can see this by calling the endpoint with e.g. postman.

It would be enaugh to have any hint how this works in JavaScript, then it will also work in angular.

epikmartin
  • 21
  • 2

1 Answers1

1

text/event-stream is the mime type associated with Server-sent Events. You can read more about them and see some code examples here and there are plenty of tutorials online showing how to use them with Angular, such as this one.

Here's a simple example of handling a Server-sent Event in plain Javascript - yourSSEURL is the URL that returns the text/event-stream:

if(typeof(EventSource) !== "undefined") {
  var source = new EventSource("yourSSEURL");
  source.onmessage = function(event) {
    document.getElementById("result").innerHTML += event.data + "<br>";
  };
} else {
  document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
TrojanName
  • 4,853
  • 5
  • 29
  • 41
  • Thank you, we use SSE already and this is not a problem. My question is, if it is possible to receive an EventStream as response from a POST request and consume Elements as they arrive instead of waiting until the complete response has been received as it is normally the case. Spring Boot is able to return an EventStream for a POST request, but JS seems not to be able to process it. – epikmartin Feb 08 '22 at 12:11
  • When you say that JS seems not to be able to process it, is there a specific error? You could add the onerror callback to capture any errors. BTW just in case you use Nginx, there is a known issue which you can resolve adding the X-Accel-Buffering: no header – TrojanName Feb 08 '22 at 13:03
  • There is no error at all, we do not even see the request in the console. This is true for all the things we tries (-> see question above) – epikmartin Feb 09 '22 at 08:47
  • So your network tab doesn't show the Eventstream connection? Try Chrome or Edge - they have much better handling of SSE than Firefox. Can you rule out a networking issue by running a browser on the same server as the backend? – TrojanName Feb 09 '22 at 11:36
  • Actually we gave up 10 months ago because we did not find a solution. We now make a "normal" POST and receive the complete list at once. Unless someone knows exactly how it works to receive the elements as stream and process elements as they arrive we will not put further effort into it. – epikmartin Feb 10 '22 at 13:38