-1

NB This is not a duplicate of this - it's about asynchronous returns.

I doubt very much whether this is going anywhere, but it could be useful so I'd like to know if there might be some answer.

I send a POST to my server (PHP) ... in response my PHP executes several MySQL queries in succession and gets some results, from each query. At the moment I'm packaging all these results up as one JSON object, echoing back, and obviously I have to wait until all these statements have been executed until I can return all results.

But I'd like to send these results back as several JSONs in succession, i.e. as the results of the queries are delivered. It appears that the AJAX mechanism can only cope with one batch of returned text (JSON or plain text or whatever) and then it ends. But it'd be nice if, instead of that, this communication line between the server and the client could "stay open".

In the Java world there's something comparable: SwingWorker, which is capable of "publishing" "chunks" of objects from a non-Event Dispatch Thread to the EDT: in effect a "wormhole" between one thread and another is kept open and it's very useful.

In the JS world it appears that the only alternative strategy to sending all results back at once is to do multiple AJAX calls... and in that case if one SQL query is dependent on the results of the previous one this is potentially going to have a big performance hit.

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • What exactly are you asking here? This reads as a brain dump of your problem with no answerable question. – Liam Nov 28 '17 at 09:49
  • Sounds like you want to store intermediate results in a memory cache, and periodically poll your PHP file for updates in the process of caching the results. –  Nov 28 '17 at 09:49
  • @ChantryCargill Thanks, yes, polling is one possibility. I suppose I was just wondering whether there might possibly be something more elegant. To continue the Java comparison, you *could* put all the results from the non-EDT process into a file and poll from the EDT... brrrrr, horrid! SwingWorker is so much better! – mike rodent Nov 28 '17 at 09:53

1 Answers1

1

Server can't initiate response for the client except it is RTC session.

So you can connect your client and server by RTC and then subscribe for all events you need.

No other way.

Drag13
  • 5,859
  • 1
  • 18
  • 42