• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

Ajax/JSON question (getting updates from webserver)

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Can a json request to a web service return replies in a stream? Or does it have to come back as one individual json dataset?

I'm working on an image upload script, and when I submit the image to the webservice, I have a series of operations that need to take place. As the operations finish, I'd like to return a json dataset to the browser that tells the client the operation has finished.

So the browser/client could get an initial reply saying the image has been uploaded, and now it's working on operation 1. When operation 1 is finished, the browser/client gets an updated json dataset pushed to it that tells the browser operation 1 is finished and operation 2 is in progress. rinse repeat. So a single submittion to a webservice would end up generating several returned json datasets that come in sequentially over a period of time.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Doing this with async http requests gets tricky. Here's a good place to start for a review of some approaches:

http://stackoverflow.com/questions/6558129/process-a-continuous-stream-of-json

Have you considered websockets instead of ajax? Also worth noting that you don't necessarily have to approach it this way. The client script could be written to repeatedly execute requests for the next image until it receives a property value in the json that signals completion, as one example.
 

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Doing this with async http requests gets tricky. Here's a good place to start for a review of some approaches:

http://stackoverflow.com/questions/6558129/process-a-continuous-stream-of-json

Thanks, I'll take a look at that.

Have you considered websockets instead of ajax? Also worth noting that you don't necessarily have to approach it this way. The client script could be written to repeatedly execute requests for the next image until it receives a property value in the json that signals completion, as one example.

I thought about having the client continually ping for an update, but I thought that might be wasteful of resources if I could instead just push an update to the client once.
 

purbeast0

No Lifer
Sep 13, 2001
53,645
6,527
126
never heard of that! I'll check it out.

yeah basically you can make async stuff ordered, so this way you can do your calls, know when they return, do something to the ui, then make the next one.

i'm doing most of my development in angular so we use the angular $q for promises. here's the docs but it's pretty straight forward to kind of understand how to use them from this, even if you aren't using angular.

https://docs.angularjs.org/api/ng/service/$q
 

mrjminer

Platinum Member
Dec 2, 2005
2,739
16
76
Looks like I will need to need to read up on nested promises, too.

For my purposes, I was going to go with a polling option with a request-specific key for status checks and ordering through a Stack on the serverside, or going through the readystate event and using the dummy bytes method, but nesting promises would be far preferable than frankensteining my own means together.