页面加载后从PHP发送GCM消息(不带ajax)

页面加载后从PHP发送GCM消息(不带ajax)

问题描述:

Hy guys!

My story:
I'm making an PHP application with Codeigniter. When my page is loaded I can click a button that calls my PHP API that makes some changes in the database and returns the result (true or false if the change in the database wasn't successful). Also after the database change I call a PHP script that sends push notifications to registered android devices that are stored in my database.

My problem:
When there are a lot of registered android devices it takes some time to load the page (PHP is waiting for every GCM request to come back). Is there a way that I can load the page after the database changes AND make GCM requests in the background/async?

EDIT #1:
I am on a Ubuntu server.

Hy guys!

我的故事: i>
我 使用Codeigniter制作PHP应用程序。 当我的页面被加载时,我可以单击一个调用我的PHP API的按钮,在数据库中进行一些更改并返回结果(如果数据库中的更改不成功,则返回true或false)。 此外,在数据库更改后,我调用一个PHP脚本,将推送通知发送到存储在我的数据库中的已注册的android设备。

我的问题 i>:
当有很多 注册的Android设备加载页面需要一些时间(PHP正在等待每个GCM请求返回)。 有没有办法在数据库更改后加载页面 AND b>在后台/异步中发出GCM请求?

编辑#1: i>
我在Ubuntu服务器上。 p> div>

There are a number of different ways to address this, but the most common solution is to use some form of a message queue to offload the work to seperate processes.

You could just store the messages to a seperate table in your database and have a cron script run every few minutes to send those messages (and only delete them from the table when successfully sent) or you could look into using rabbitmq, gearman or beanstalk which are designed to be more robust and more easily scaled.

Recommended reading:

If you can separate the push code in to it's own standalone script, you could call it with

 exec("php /path/to/script.php > /dev/null &");

This should run it in the background (on Linux) without the script that calls it waiting.

Another option might be to store notifications in the database as a queue and have a script run via cron every N minutes to check the queue and push notifications from it.