开发者

A facebook-like-notification system in php

开发者 https://www.devze.com 2023-01-13 14:36 出处:网络
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

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 interfac开发者_JAVA技巧e, the message appears instantly

How can I do that in php?

Thank you very much,

Regards


I wouldn't advise using either polling or trying to implement a push based solution using PHP, if you are going to have any sizable traffic. What happens is that eventually all the PHP processes get blocked, and then you can't serve anymore web requests.

Look into node.js, cometd or another push based solution. If you need something simple and need to get up and running quickly, I'd recommend http://pusherapp.com/. They have a PHP client available and super simple API.


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.


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.


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:

  • http://php.net/manual/en/function.stream-socket-client.php
  • http://php.net/manual/en/function.stream-socket-server.php

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.


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.


Very much possible nowadays. I know this answer is coming very late but it will help those who will view this page for help. Check the link below for a great tutorial Real Time Notification Like Facebook


Nowadays the best solution is to use socket.io (http://socket.io/). It automatically chooses the best (and availible!) way to transfer data, including: WebSockets, Adobe Flash Socket, Ajax polling. You really don't worry about availability of communication methods on the client side.

0

精彩评论

暂无评论...
验证码 换一张
取 消