views:

102

answers:

4

Hi everybody,

How can I put in place a facebook-like-notification system: - A userA writes a message to the userB - A listener on the database routes the message to the userB - On the userB interface, the message appears instantly

How can I do that in php?

Thank you very much,

Regards

A: 

You could do this one of two ways. In PHP the easiest way would be 'polling'. The newer neater method is using HTTP Push with a Comet server; but that's not so great for PHP.

To use polling ou simply update a table in the database with UserA's message flagged as to-be delivered to user b. You have a script running every few seconds via ajax that polls the database for the message on userb's client. If the message is there you populate it. This is very inefficient.

akellehe
A: 

I have a system that works well set in place. It basically has the set up like this:

notification_id
user_id
module
action_type
added_by
read
read_date
added

This is a simple way of doing it. user_id is who it's for, added_by is the user id of whomever performed the action. Module is where it was performed at, and action_type is what happened (comment, deletion, added, etc). Read is if it's already been viewed, and read_date is when it was viewed.

I then have a class that builds the verbiage based on what the values above are.

I have then set up a crontab that will clean up old notifications in the database after so many days.

bradenkeith
A: 

Comet server is not so simple to write in PHP, but it is possible to do so using sleeps. There are also socket functions that can be useful:

Note that you should be aware of time limit. Maybe you should stop hanging after some time and make a new request, unless you can disable time limit for these requests.

The solution may also depend on the environment. You probably can't do many useful things on usual shared webhosting.

v6ak
A: 

As mentioned by akellehe, you could simply poll using Ajax to look for new content if your users are likely to remain on the same page for long periods of time. If absolute real-time delivery isn't a necessity, it's a lot more efficient to check for new notifications when the page reloads -- especially if you cache (e.g. memcached) the total events awaiting a user and only invalidate that count in the event something is added to their notification queue. This will save you a lot of empty-handed database queries.

Jeff Standen