Dhiva Banyu Wigara
Dhiva Banyu Wigara

Reputation: 39

Get update from mysql as soon as a new insert

I have a webshop, user order's will be saved on mysql and it will be process later in batch by "processor.php", it will be processed later because the requirement to send this order to other server is using a chat application

processor.php is a jabber bot, php bot that will be running 24/7 and it will check at db every 5 minutes and process the orders (send it to another bots, and update the orders based on the other bots reply)

I'am wondering if there is a way so processor.php can have a notification like "hey there is a new order, process it" from mysql every time a user submit a new order, something like a real time

Upvotes: 0

Views: 59

Answers (1)

Nerdi.org
Nerdi.org

Reputation: 923

The best you can do is edit the file that INSERTS the data in the first place.

You can use $query->insert_id; or a myqsli equivalent if you need the ID of the new order

You can then pass this ID to the processor. Believe me, relying on crons can become a nightmare and with payments and orders I highly recommend avoiding a cron solution.

That said, if your processor only changes things in the DB itself and nothing else you can use internal MySQL triggers!

https://www.siteground.com/kb/mysql-triggers-use/

But if your processor does non-DB work you will have to either:

Use your cron

Set up the order insert to then also trigger the processor on it's own without using a cron.

Set up a function that looks to see if order is ready for processing on a page a user visits, so that their order can begin processing without any delay.

You can use AJAX or even PHP to call processor.php. you can even pass it a specific ID. Essentially any basic method like file get contents, $.get(), or any other programming function (in any language) will let you call processor.php and have that file run on your web server. You can do this from any language that supports requesting a file from the same or different server (so most).

I highly recommend making sure you set up proper permissions and even a basic API so only YOUR server(s) and service(s) can trigger the processor!

Upvotes: 1

Related Questions