I have a chat server waiting to start a new thread with two participants. I also have an application server that receives the first request for initiating chat from one of the clients. The other client is decided by who is waiting in SQS (A Queue). Assuming that I can get the two participants from SQS, how do I send an async http request to the chat server with both the client id parameters? I need the code part that makes the async call from this server to the chat server with the two parameters
Asked
Active
Viewed 359 times
0
-
1See [here](http://stackoverflow.com/a/962920/5684195), this should exactly give you the answer you are looking for. – Happy Dec 18 '15 at 21:31
-
file_get_contents/curl? there isn't really "async" in php. it's not a threaded language, and pretty much all operations are purely serial/blocking. – Marc B Dec 18 '15 at 21:31
-
there are "hacks" though. it can be done! – I wrestled a bear once. Dec 18 '15 at 21:35
-
I did not know that all processes in php are blocking/serial. and thank you @pamblam for your answer. If there is no true async, I'll have to rethink my application as I cannot afford to waste server resource (EC2 instances are costly!) but thank you for your hack answer. This should help someone who is looking for this. – Prasana Ramesh Dec 18 '15 at 21:54
-
1When you hear that PHP is blocking, this is true. This does not apply here though because we're talking about two different scripts interacting with each other **asynchronously**. people love to correct people who say PHP is asynchronous. It's not, but the request is, and that's what you were talking about. No resources are wasted by using something like this. – I wrestled a bear once. Dec 18 '15 at 22:01
1 Answers
1
Here's a little class I use to do this.
/**
* The basic class for handling async PHP requests
* @author Rob Parham 10/26/15
*/
class asyncPage{
/**
* To be called that the start of any page that is intended to
* return content to the browser and close connection before
* stopping excecution.
*/
public static function startOutput(){
ignore_user_abort(true);
set_time_limit(0);
ob_start();
}
/**
* To be called after sending output back for the browser and before
* executing any long-running code
*/
public static function sendOutput(){
$contentLength = ob_get_length();
header("Content-Length: $contentLength");
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
if (session_id()) session_write_close();
}
}
Use it like this on your page that is receiving the request..
// Start the output buffer
asyncPage::startOutput();
// Send some junk back to the browser
echo "processing your request";
// Close the connection and send the output before script is done
asyncPage::sendOutput();
// execute your long process here
sleep(60);

I wrestled a bear once.
- 22,983
- 19
- 69
- 116
-
I understand having both `ob_end_flush()` and `flush()`, but why the `ob_flush()` also? – alzee Dec 18 '15 at 21:52
-
From the manual: flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those. – I wrestled a bear once. Dec 18 '15 at 21:55
-
I just meant that `ob_end_flush()` implicitly calls `ob_flush()`; I thought you only needed one or the other, alongside `flush()` to overcome any buffering on the server. – alzee Dec 18 '15 at 22:01
-
Idk you might be right, ill have to check the manual when im back at my computer. – I wrestled a bear once. Dec 18 '15 at 22:17