Execute PHP script automatically whenever database changes - php

THE ESSENCE
I have a table in my database (SQL Server) and a WCF service that communicates with DB to affect it. I also have a PHP script on my server. Now whenever my table has some data added, modified or deleted I want my PHP script to be executed automatically.
ITS APPLICATION
I am working on a mobile application that is almost completed. Now I need to implement a push notifications feature. I.e. whenever there is a change in the database I have to run my server side script that is configured to push a notification to the user.

Push servicing should be implemented in the layer that adds the data to the database. A database is merely for storing information not for executing code. So in order to create push notifications you would need an interface that receives the updates, sends it to the database and pushes it to the users.
Seeing your question, I certainly don't hope you allow your app to directly add information to the database without some layer that validates incoming information. That's a direct security risk.
A solution of a model would be:
App -> send information -> Website -> analyzes request in PHP/other code languages -> inserts in Database -> push message to clients.

You don't say what RDBMS you are using but whatever one it is, you need to read the documentation about "triggers". This should give you all you need to know.

Most of the time this is done using cron (or another scheduler) to check a database on a certain interval (every minute for example) to find new work and then process it.
However, that sort of design only gets you so far and the next stage would be to move up to using a message queue (Like gearman, ZeroMQ, etc).
You might be able to make something work with triggers, but generally, a database should not be treated as a queue.

Related

Can I use Laravel 4 Queues to process print-jobs on Windows?

This question is a continuation to questions I have asked previously with regard to printing documents via Word on Windows from Laravel.
My issue was that I did not want to launch the necessary printing tasks within a POST request as this would show no feedback of the task, and would only respond once the task completed.
For example, if I called the POST /pledge/submit route, I would not want to call the necessary tasks for printing within that same request for the route.
Now, I see that Laravel 4 has a facility called Queues, which (I assume) would allow me to background process these tasks, and postpone them until a later time.
Having read through the documentation, I see that it supports four different drivers, one of which is sync.
Question: Can I use this driver to create new print jobs in the queue, and have them executed by an external application (such as one created in Delphi)? The app would periodically check to see if there are items in the queue, and then execute them (and, of course, remove them).
I am simply trying to find the best way to publish documents without the end-user having to wait for the page to respond whilst printing is underway. Further, I am new to queues in PHP, and am not familiar with how they work (in so far as a detailed process flow). If someone could also explain this, I would appreciate it very much.
The queue system wouldnt work for your Delphi program out of the box - you would need to make some modifications.
Instead - the easiest way would be to make your own 'table' in your database, called 'pending_print_jobs'.
When the user wants to print job 'x' - you get PHP to save the print job in the 'pending_print_jobs' table with all the details you need (such as file to be printed, the user who did it etc etc).
Then you would get your external application (i.e. your Delphi program) to periodically check the 'pending_print_jobs' table in your database. If it finds any records - it can action them - and print the file.

MySQL callback to the web-app

I'm trying to figure out the best way possible to notify my web-application of database changes. The way I'm doing it now, is that I've got separate MySQL table with a counter.
Now, when some database action happens in table Foo, a trigger is adding up the counter.
The web-app then polls every 5 seconds the server to check out, if something has happened (counter number has changed) and if so, refresh the data in app.
What I would like, is that I would be able to do callback/notify from MySQL to the server and from there to the web-app so that I don't need to poll the server frequently. Is this possible somehow?
How does facebook, gmail send the real time notification?
You can't notify your application directly from MySQL, but there are some solutions to save bandwith and load of your server.
one way of handling this - would be to either implement the observer pattern yourself or simply use a pubsub messaging option (ZMQ/AMQ/RabbitMQ/Redis etc) - when the initial database action takes place (ensure that the transaction has committed), publish the message to the topic on the pubusb tool - your application can subscribe to the pubsub tool and receive messages when there is a DB change.
Hope it helps.

Sending a notification when a MySql database entry changes

I've written a php file that changes a MySql table entry when it receives an http post. I would also like the php file to send out a notification to the table entry's owner. This idea is similar to a chat room or instant messengering program. I've looked at php chat scripts but I really need something that has a very simple interface that is customizable. Can anyone point me in the right direction?
So you want to synchronize a set of clients, do you?
If so, look at the Long Polling technique. It's quite simple: The client opens a connection but the server does not respond until data is updated.
On the downside this won't work well with PHP. You will need to sleep() several connections, therefore blocking PHP processes.
If you have the possibility I would recommend using node.js to do stuff like that. Long Polling Chats are quire simple to implement using node ;)
I would use a named look for event triggerning and a jabber bot (extensions exist for several languages).
http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/

Connect PHP with Orbited

After searching the web for a good Comet and also and asking you guys what my best option is, I've chose to go with Orbited. The problem is if you need a good documentation about Comet you won't find. I've installed Orbited and It seems It works just fine.
Basically, I want to constantly check a database and see if there is a new data. If there is, I want to push it to my clients and update their home page but I can't find any good and clear doc explaining how constantly check the database and push the new info to Orbited and then to the clients. Have you guys implemented that?
Also, how many users can Orbited handle?
Any ideas?
You could add a database trigger that sends messages to your message queue when the database got changed. This is also suggested here. Or, if it is only your app talking to the database, you could handle this from within the app via a Subject/Observer pattern, notifying the queue whenever someone called an action changing something in the DB.
I don't know how good or bad Orbited scales.
Have a reference table that keeps track of the last updated time of the source table. Create a update/delete/insert trigger for the source table that updates the time in the reference table.
Your comet script should keep checking the reference table for any change in the time. If the change is noticed, you can read the updated source table and push the data to your client's home page. Checking the reference table in a loop is faster because the MySQL will serve the results from its cache if nothing has changed.
And sorry, I don't know much about Orbited.
I would use the STOMP protocol with Orbited to communicate and push data to clients. Just find a good STOMP client with PHP and get started.
Here is an example of a use case with STOMP, although the server side is written in Ruby:
http://fuglyatblogging.wordpress.com/2008/10/
I don't know if PHP with Apache (if that's what you are using) is the best suite for monitoring database changes. Read this article, under the section title "Orbited Server", for an explanation: http://thingsilearned.com/2009/06/09/starting-out-with-comet-orbited-part-1/
EDIT: If you want to go the route with PHP through a web server, you need to make one, and one only, request to a script that starts the monitoring and pushes out changes. And if that script times out or fails, you need to start a new one. A bit fugly :) A nicer, cleaner way would be, for example, to use twisted with python to start a monitoring process, completely separated from the web-server.

Transaction Processing Using PHP and MySQL

I'm trying to implement two-phase commit using PHP and MySQL, and coming up short.
The main block I've found is that I'm unable to store the MySQL connection resource in a place where I can find it again for the second phase. Is it possible to serialize a database handle?
Here's the case I'm trying to code for:
User sends data
Server starts a MySQL transaction and executes some queries based on the data it has received.
Server sends a file back to the user
When the user has successfully received the file, the server commits its transaction. Otherwise it rolls it back.
This seems to require two HTTP Request/Response cycles, so I need to be able to re-connect to the same database handle in the second request in order to commit the transaction. I've been failing at this part.
Any advice is welcome, even if it's "this is not possible in PHP"
Take a look to LIXA Transaction Manager (http://lixa.sourceforge.net/) it integrates PHP and MySQL starting with release 0.9.0
It provides Distributed Transaction Processing and two phase commit feature as well.
Regards
Ch. F.
Since php is Request / Response based the implementation of a persistent DB connection is not possbile, AFAIK.
You could try to work around this limitation using sort of a ticketing mechanism. Your steps would be:
User sends data
Server starts a MySQL transaction and executes some queries based on the data it has received, assigning a 'unique' ticket to that transaction.
Server sends a file and the ticket back to the user
When the user has successfully received the file and sent another request containing that ticket, the server commits its transaction. Otherwise it rolls it back.
refering to Cassy's comment: after a certain period of time all not commited TAs should be rolled back in order to prevent your db from beeing 'flooded' with old transactions
HTH
to answer KB22 and rojoca, the reason I need to do it this way is that the 'file' i'm referring to is actually a sqlite database that ends up as a data store on a mobile device.
The first request posts the updated sqlite database to the server, which attempts to merge in data from the sqlite tables; problems arise when the mobile device doesn't successfully receive a new sqlite database (one which reflects the mobile device's changes and any other new stuff from the web application), because it will then attempt to send the same (old) sqlite database to the web a second time, resulting in duplicate entries in the web tables for anything which was created on the mobile device.
So, the web needs to be sure that the device has the new database before committing the merge changes. Given the vagaries of networks, this only seems feasible if the device can send an explicit ACK after receiving the new sqlite database. And this is only possible if we make two requests (1. The sqlite database to merge; 2. the ACK of receipt of the new sqlite database on the device).
A thorny problem indeed, and it's useful information to find out that PHP can't manipulate database handles down to the necessary level.
[I also don't think I can use a transaction table because I need to return data to the device based on the 'real' web database tables. I think i'd run into issues with auto_increment fields if I didn't use the real tables]
Thanks for all your comments.

Categories