Home > Mobile >  How to get streams of data in angular
How to get streams of data in angular

Time:11-07

Trying to get a stream of data displaying on my console, but 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" }

Tried to use the EventSource method, but that doesn't even manage to connect it. Proxies work fine. Any ideas? Thanks very much in advance.

CodePudding user response:

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.

CodePudding user response:

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 currently uses XMLHttpRequest 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.

  • Related