3

the situation i'm facing is:

i have a php+mysql web application. several users share resources in the same database. i want to implement a feature that whenever there is a change for database records(create,update or delete), other users will be notified instantaneously.

I'm considering to use ajax push engine (APE), but not sure how the big picture is.

(1) is it something like i need a script constantly checking the last_update timestamps and send notifications to client when a new change is detected?

OR

(2) every time after performing any operation to database table, send notification to clients, telling them to reload data?

which one is better or any other suggestions on how to implement this?

thanks in advance!

Yang
  • 9,794
  • 15
  • 44
  • 52
  • It depends on if you have to do any conflict resolution as a result of them trying to update stale data. If so, you may need to invalidate their session and somehow require they grab the fresh data. That being said, you should be able to refresh the data for them if you institute what you described in point (1) above, by setting a polling interval on a service that provides the status of whether or not a change has been made. If that returns a different checksum, hash, max key or last change timestamp, for instance, you know to grab new data. – philwinkle Feb 15 '11 at 05:16

2 Answers2

2

Your best option is #2, when you write to the database that is when your change is happening, that is when you should push.

class DatabaseModel
{
    ...

    function save($data)
    {
        ... // your db query

        after_save();
    }

    function after_save()
    {
        // push changes out using some other object 
        // e.g. MyPushClass::something_changed($this);
    }
}
esfourteen
  • 491
  • 5
  • 6
1

You can use comet library for this.

http://ajaxian.com/archives/comet-with-php

http://www.zeitoun.net/articles/comet_and_php/start

Nik
  • 4,015
  • 3
  • 20
  • 16