简体   繁体   中英

Angular 6 - how to make a single http request and listens to multiple responses?

My backend generates log on processing some data and i would like to show it as a console in my frontend.

How can i implement a method that can listen to multiple response till a certain parameter is true from backend on a single http request in angular 6.

you can make use of WebSocket, ie make websocket connection with the your backend and get data, this is kind of push mechanism where server push data to on connection and client get data as new data is available in connection.

it not possible with help of single http request as it follows pull mechanism. so you will get data which are available. to get new data you have to perform another http request.

Unfortunately, an HTTP request cannot remain open listening for multiple responses, once it receives a response it will close the connection.

Fortunately, you can use websockets .

Implementing websockets is not too difficult, and there are many tutorials for implementing with Angular such as this one: https://tutorialedge.net/typescript/angular/angular-websockets-tutorial/

I'm not sure what back end technology you're using, but most modern ones have websocket support.

If you're not familiar with websockets in general, checkout this article: https://medium.com/@dominik.t/what-are-web-sockets-what-about-rest-apis-b9c15fd72aac

“WebSockets” is an advanced technology that allows real-time interactive communication between the client browser and a server. It uses a completely different protocol that allows bidirectional data flow, making it unique against HTTP.

The article also compares/contrasts it to HTTP, so it may give you a better understanding of HTTP as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM