0

I am trying to get a stream of data displaying on my console, but I'm probably not aware of the right tools. If I try to get the data from curl it works fine and looks like this:

C:\Users\user>curl localhost:8080/slow/5
5
4
3
2
1
0

C:\Users\user>

First I tried to use a standard http get request,

constructor(
    private http: HttpClient,
    private ngzone: NgZone
) {
  this.http.get('/slow/5').subscribe(x => console.log(x))
}

and this is the error I get after 5 secs:

error: Object { error: SyntaxError, text: "5\n4\n3\n2\n1\n0\n" }

I tried to use the EventSource method, but that doesn't even manage to connect it. Proxies work fine. Any ideas?

2 Answers 2

1

Angular's HTTP client does currently not support streaming. There was a feature request for this but it was declined. The HTTP client of Angular uses XMLHttpRequest internally which does also not support streaming.

If you still want to stream the response data, you will have to use the native fetch API which does support streaming. See MDN.

Sign up to request clarification or add additional context in comments.

Comments

0

Please check this Stackblitz example where a dummy stream of data is setup using the of operator from RxJs.

You can see how that stream of data is subscribed in the app.component.ts, in order to print out its values in the console.

1 Comment

It's a bit different from my case, coz here the stream is already present. In the curl console a value if printed every second, which means that angular needs to listen to the source for 5 secs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.