0

I recently started working on ionic framework using angular JS. Here is my problem with $http.post

My requirement is I need to upload photos to my server. User selected bunch of photos (say 15), and starts uploading to server. Here is my code for uploading to server

foreach(photo in photoList){
     $http.post(url,photo).then(success(){},error(){})
}

Now my problem is out of 15 only 6-7 photos are uploading. For remaining photos $http.post() calls are not even getting called. I heard there might be $http concurrent issues. Is that correct? If so how to resolve this issue?

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107

1 Answers1

0

You are being faced with a concurrency limit built into all web browsers by convention. Browsers will not make more than a certain small number, usually 6 (details here) of simultaneous connections to the same origin host. You cannot bypass this in javascript or with any technology available to a web application. It is for good reason as at some point the parallelism actually results in lower overall performance as the network saturates or other resource limits start to be hit. You must work within this limit. The browser will automatically queue your requests within the concurrent threshold, so you don't need to add any special code. However you can if you so choose impose your own concurrency limit in code if you want to provide more accurate progress to the end user.

Community
  • 1
  • 1
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • Thanks for your response. You mentioned- "The browser will automatically queue your requests within the concurrent threshold, so you don't need to add any special code" - But I am not seeing this happening. Actually I am working on PhoneGap application which runs on browser.I am initiating 15 $http.post() sitting in for loop. But only 6 out of 15 are getting back. Rest never get back. If browser automatically queue then rest should get executed after initial 6 was completed. But that is not happening. – Vamsidhar Mamillapalli Dec 10 '14 at 05:28
  • That could perhaps be untrue about AJAX requests or non-GET ajax requests. You'll have to experiment a bit. I know it is definitely true for other resources such as images. I don't know the exact rules and limitations for your specific use case. Let us know if you get details figured out or maybe someone else will post an answer. – Peter Lyons Dec 10 '14 at 18:44