How do i set up a facebook-like-notification system: - A userA creates a note towards the userB - A listener around the database routes the content towards the userB - Around the userB interface, the content seems instantly
How do i do this in php?
I would not advise using either polling or attempting to implement a push based solution using PHP, if you are planning to possess any substantial traffic. Ultimately that eventually all of the PHP processes get blocked, and you can't serve any longer web demands.
Consider node.js, cometd or any other push based solution. If you want something easy and want to get ready to go rapidly, I'd recommend http://pusherapp.com/. There is a PHP client available and super simple API.
You could do this that one of two ways. In PHP the simplest way could be 'polling'. The more recent neater technique is using HTTP Push having a Comet server but that is not too ideal for PHP.
To make use of polling ou simply update a table within the database with UserA's message flagged regarding-be shipped to user b. You've got a script running every couple of seconds via ajax that polls the database for that message on userb's client. When the message can there be you populate it. This is inefficient.
I've got a system that work well occur place. It essentially has got the setup such as this:
This can be a simple method of doing the work. user_id is who it's for, added_by may be the user id of whoever carried out the experience. Module is how it had been carried out at, and action_type is exactly what happened (comment, deletion, added, etc). Read is that if it's recently been seen, and browse_date happens when it had been seen.
Then i possess a class that develops the verbiage according to exactly what the values above are.
I've then setup a crontab which will cleanup old notices within the database after a lot of days.
Comet server isn't so easy to write in PHP, but you'll be able to achieve this using sleeps. You will find also socket functions that may be helpful:
Observe that you should know of your time limit. Perhaps you should stop hanging after a while making a new request, unless of course you are able to disable time period limit of these demands.
The answer could also rely on the atmosphere. You most likely can't do many helpful things on usual shared webhosting.
As pointed out by akellehe, you can simply poll using Ajax to search for new content in case your customers will probably remain on a single page for lengthy amounts of time. If absolute real-time delivery is not essential, it's much more efficient to check on for brand new notices once the page reloads -- particularly if you cache (e.g. memcached) the entire occasions waiting for a person and just invalidate that count in case something is put into their notification queue. This could save you lots of empty-handed database queries.