1

So I've built an app that uses an API.

When someone uses that API to submit some data to my database I would like them to be able to continue using the application as fast as possible. Now my problem is when they submit data I want to inform other users that the user has done this. The best way to achieve this in my eyes was push notifications.

However when they have submitted the data I go to collect all users that I want to send a push notification to and loop through them, if there are a lot of users the API will take a lot of time before the function has finished executing.

I want to be able to send data back to the client and still continue executing the PHP script since it has no use for the client to be waiting till this has finished. Is it somehow possible with PHP to asynchronously send data back and still continue executing the script?

M1ke
  • 6,166
  • 4
  • 32
  • 50
Sjoerd de Wit
  • 2,353
  • 5
  • 26
  • 45
  • I suppose you could implement an [observer pattern](http://en.wikipedia.org/wiki/Observer_pattern), it's my no means async. It's the only thing I could think of that doesn't involve javascript. – Andrei P. Mar 04 '15 at 10:59

1 Answers1

0

There are a number of ways to create async messages in PHP.

  • Use an event driven library such as React
  • Use ZeroMQ (with or without React) to send messages via TCP sockets to another service.
  • Execute a background shell script using shell_exec("your-cmd > /dev/null 2>/dev/null &") where your-cmd is a script or other executable; the rest ensures that PHP lets the process run in the background.
  • Make an asynchronous HTTP request as [detailed in this question](Asynchronous HTTP requests in PHP
Community
  • 1
  • 1
M1ke
  • 6,166
  • 4
  • 32
  • 50
  • i'm gonna try the background shell script way and see if i can get it working. Thanks :) – Sjoerd de Wit Mar 04 '15 at 13:42
  • Great, if it works please remember to come back and accept the answer and I will edit to mention this was the working solution. – M1ke Mar 04 '15 at 14:47