1

I have a web service written in PHP to which an iPhone app connects to. When the app calls the service, a series of notification messages are sent to Apple's APNs server so it can then send Push Notifications to other users of the app.

This process can be time consuming in some cases and my app has to wait a long time before getting a response. The response is totally independent of the result of the notification messages being sent to the APNs server.

Therefore, I would like the web service to send the response back to the app regardless of whether the messages to APNs have been sent.

I tried using pcntl_fork to solve the problem:

<?php
...
$pid = pcntl_fork();
if($pid == -1) 
{
    // Could not fork (send response anyway)
    echo "response";
} 
else if($pid) 
{
    // Parent process - send response to app
    echo "response";
} 
else 
{
    // Child process - send messages to APNs then die
    sendMessageAPNs($token_array);
    die();
}
?> // end of script

Unfortunately, the parent process seems to wait for the child process to end before sending the response even though I do not use pcntl_wait in the parent process. Am I doing something wrong or is this normal behaviour? If this is normal then is there another way I can solve this problem?

Thank you!

rickholt
  • 13
  • 3

2 Answers2

0

I think you can send the response back before doing the "long" process. Take a look at the flush() function of PHP it'll maybe help

Nettogrof
  • 2,116
  • 2
  • 15
  • 22
0

If you're hosting the PHP process in Apache then you really shouldn't use this: see this for the section that says *Process Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web server environment. *.

You should probably set up a separate daemon in your preferred language of choice and hand the APNS communication tasks off to that. If you really really really must try using ob_flush().

Femi
  • 64,273
  • 8
  • 118
  • 148
  • I tried ob_flush() followed by a flush() since it seems the former is not enough to guarantee the response gets sent. Unfortunately this also sent info about the PHP backend in the response. The daemon method is probably the best way to go, but I didn't have time to implement it so I sent a second request to another web service from my app to handle the notifications separately. – rickholt Sep 23 '11 at 00:01