What happens if I make the same $http request - same params, etc- simultaneously in the app? Does one wait for the others' response?
And if I make them back to back. If the first one is not resolved yet, do the others wait for that?
There is a limit for browsers request count See Max parallel http connections in a browser?
Think each request independent , angularjs $http service return a promise object and you register its success and error callbacks.
Read more about from here https://docs.angularjs.org/api/ng/service/$http
See my plunk below. If I call multiple calls I see them parallel in network tab. But this doesn't cause a conflict because we have different promises and their callbacks will be different. call 1 promise instance1 ... call N promise instance N
http://plnkr.co/edit/XUboyUiAs7KFcRhUu3LP?p=preview
Related
I have a few components in one page.
Each of them fetches the same data from the server.
As a result, when the page loads, these components send the same request multiple times.
Is there any way to prevent this? Like caching the promise of the first request and returning that to the next coming requests (before the promise resolved)?
In order to make sure that the request is sent only once, you can keep track of the first HttpPromise you create, and on subsequent calls of the function, return that same promise.
This SO link might be what you're looking for.
When calling the $http service you can additionally supply a cache object. If you do so any additional requests will use the cached value. If the same cache is used then additional requests made before the first is resolved will not call the server but wait for the response.
$http.get(url, {cache:cacheObj})
Where cacheObj is from $cacheFactory
In my angular app I have an http interceptor that will perform some operation fn(x) on every response from the server. The problem I have encountered is that the angular $http service will still hit the interceptor when it is returning a response from its own cache. So, because one of my $http calls is using cache = true in it's config, I end up executing fn(x) on calls that do not reach the server.
Is there any lower level way in angular to determine whether the response actually came from the server rather than from angular's cache?
For this method call:
$http.get("Some api call").then(function (response) {
$scope.data=response.data;
});
Suppose the response keeps on updating from time to time and I wish to update the $scope.data property whenever the response is updating without firing the $http.get using timeout or interval methods.
I am not getting any solution for this issue. Please provide your ideas with examples.
What you are asking is called Polling.
Your HTTP request will NOT get the latest data unless you fire the request again. Unless you fire the request again, your response will not have the latest data and thus, your data object will also not get updated.
No amount of $watch will suffice. Simply because, unless you poll the server periodically, you will not get the latest data.
This is how HTTP/1.1 behaves. This is soon about to change with HTTP/2
If you do not wish to make periodic requests, then have a look at sockets. You have not mentioned your backend but a Nodejs example can be found at socket.io.
a) What are the difference between $http and $q ?
b) When should $q be implement over $http and vice versa ?
c) When and best practice for implement $http and $q at the same time?
a) $http executes HTTP requests in an asynchronous manner, which means that you can not be sure about the time when you'll get an answer from the server. $q is a service that provides you the capability to execute multiple asynchronous tasks one after another. That being said they conceptionally do have nothing in common.
b) Consider a situation where you want to have multiple async HTTP calls to a server. You may have the possibility to nest each of those calls (for instance making the 2nd call in the success callback of the first call). However you find yourself in situations where you have various amounts of calls. You would then use $q to circumvent nesting code.
c) Whenever you have a single HTTP call you should use $http. Whenever you have numerous calls, you should use $q.
a)
$http = angular service for access a server via the http protocol.
$q = angular service implementing kris kowalkis q library https://github.com/kriskowal/q.
They are both angular service but have nothing else in common.
b)
$http uses $q to provide defered access (promises). But I know no situation where i would use $q over $http. As far as you want to make http requests.
c)
$http uses $q. So they are always used together. As long as you want to make http requests.
I am currently building a dashboard page with multiple widgets. Those widgets retrieve their data with REST calls ($resource). A few widgets make similar calls and I don't want to DDOS our server so I am looking for a way to make a call only once and resolve all similar requests with the same response.
Since I am restricted to using POST requests only, I cannot use the cache option that $resource offers. This seems to be doing exactly what I want but only for GET requests.
I was thinking along the lines of using a http interceptor to queue similar POST requests, fire only one of them and resolving them all when the first one gets its response.
However, I cannot seem to put the pieces together so any help is appreciated. I am open to other options.
Kind regards,
Tim
Services in AngularJS are singletons, so a solution would be to store the response in the service, as a variable. Then next time you'll do the request, previously check if the variable is null, if it's not you wrap it in a promise and returned it. If it's null, then you do the request, and store the response for the next call.
You can also either use this in your request service or in your interceptor service.
I hope I helped !
Refactor your widgets to depend on a service (singleton).
This service should either poll the server via XHR, or get server push via websocket for updates.
If polling, look into server side caching and etags.