Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter RESTful, async / background process

I'm using codeIgniter RESTful API (https://github.com/philsturgeon/codeigniter-restserver) that return information (json format) to my android/iphone app.

There are an operation where i send some values, if it is everything OK i return 200 code as response.

Now, i want to add a new operation at the same method: send notifications of this modifications with APNS (Apple Push Notificacion Service) and GCM (Google Cloud Messaging).

It works well when i have to send no more than 3-5 notifications, the problem is APNS, because i have to send this messages one by one and it takes a long time, so my apps recieves a timeout exception (all the notifications are sent but the user get the Error Connection...)

Can i send the 200 code response and then continue sending this notifications? (Something like this...)

function my_update_method_post(){
   //....GET my POST values
   update($data);
   $this->response(array('result'=>1),200));


   //Send Notifications
   ....
}

Thanks in advance...

like image 275
jzafrilla Avatar asked Oct 05 '22 19:10

jzafrilla


2 Answers

I found a solution that works perfect for me because i don't expect any result value. If notification can't be send...i log it in my database.

This is the function that i use to send "async" request (yes, This is not an asynchronous request, but it works how i'm looking for)

function curl_post_async($url, $params)
{
    $post_string = http_build_query($params);
    $parts=parse_url($url);

    $fp = fsockopen($parts['host'],
        isset($parts['port'])?$parts['port']:80,
        $errno, $errstr, 30);

    if(!$fp)
    {
        //Perform whatever logging you want to have happen b/c this call failed!    
    }
    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}
like image 160
jzafrilla Avatar answered Oct 10 '22 23:10

jzafrilla


Yes this is possible.

You should look at PHP exec() and this link. You should set up a function in your controller to be called from the command line. you will then pass in an array of the GCM/APNS data to be used.

This solution is not ideal because you won't be able to tell the client that all message were sent successfully. You will send back 200 to say the request was received ok and that is all.

like image 39
jim Avatar answered Oct 10 '22 22:10

jim