Skip to content Skip to sidebar Skip to footer

Automatic Notification In Database Change :similar With Facebook Friend Request

i wish to develeop a php mysql based social networking site. Registered users will have the option to add another user as a friend just as is done in Facebook. If user A clicks on

Solution 1:

To finalize the answers, I assume that you understand the database/notification DATA logic well, and now you are concerned only about HOW TO deliver it to the browser. I will list all the points here.

  1. HTTP is a PULL protocol. You cannot push data from Server to Client.
  2. So, what you can do is - continuously poll the server for new notifications.

You can do two types of polling :-

  1. Short-polling - You send an Ajax request to the server for new notifications.
    • This is a short request, but you do it continuously in an interval (say 10s)
    • The server process the PHP and immediately returns.
    • You process the returned data (eg. Show notifications)
  2. Long-polling - You send the same request as above. BUT...
    • In this, the requested PHP file goes into an infinite loop, checking DB continuously.
    • When a DB change is there, the PHP file "echo" it and ends the loop. Now the request is completed, and data is received at Browser.
    • You process the data.
    • Start another long-poll Ajax request, and the steps repeat again.
    • (Extra: If the PHP is not completed in a particular timeout, then abort the current request and start new one.)

Next, you can implement any of these in two different ways:-

  1. Write your own Javascript code - Needs some expertise, but you can learn it!
  2. Use some libraries - Easy, and time-saving. You can use PubNet, BeaconPush etc.

I hope that this makes a clear idea to you!

Solution 2:

you need to write javascript that sends request to server after each interval of time. and then on the server side you can query the database and respond to the client if there are any new friend requests.

use the javascript setinterval function

var refreshId = setInterval(function(){  
$.ajax({
type: "POST",
url: "request.php",
data: 'more_updates='+more_updates, // or any data you want to sendcache: false,
success: function(html){
$('.old_updates').prepend(html).fadeIn('fast'); // response from server side process
}
});
}

},10000);

here we are sending data every ten seconds. so on the server side if we have any new friend request pending that are new or not confirmed it updates the client side.

Solution 3:

First, you don't need to keep separate databases/tables for each people. You can keep a single database table with the following columns:

table_friendship

+------+---------------+-------------+------------+
|  id  |  friend_from  |  friend_to  |  approved  |
+------+---------------+-------------+------------+
|  1   |      A        |      B      |     NO     |
+------+---------------+-------------+------------+
|  2   |      C        |      B      |     NO     |
+------+---------------+-------------+------------+

Here in this table, the entry 1 means, A requests B for friendship, and NOT approved. Entry 2 means, C requests B for friendship, and NOT approved yet.

Next step, is to notify "B" that two requests have been arrived at the server. Sadly, the server cannot send messages to the client (Browser). So what we are doing is, continuously poll the server through AJAX request. The request works like any other page request, but the difference is, you can request a PHP page without reloading the browser.

You can request the page friend_requests.php in an interval of 10 seconds. The page should do the following:

  1. Do the SQL "SELECT COUNT(*) FROM table_friendship WHERE friend_to = B AND approved = "NO" (Just a pseudo-SQL only!)
  2. Return the following data: Number of requests pending, and New requests etc.

At the Browser side, you can display this data in the "BOX" that you have specified.

When you approve the request, you again send the AJAX request to another PHP page, to change the approved column to "YES"

More on Ajax - en.wikipedia.org/wiki/Ajax_(programming)

Solution 4:

I think you are searching for a push notification service.

You can either implement your own (using Comet), or subscribe to a public service.

Examples:

PubNub, BeaconPush

You will find a lot more with google.

Edit

I think my answer was not clear enough. With my suggestion, you could do this (using pubnub):

User B (user ID 7) writes a friend request to user A (user ID 8). In your PHP handler you do:

$pubnub->publish(array(
    'channel' => 'friend_requests_8',
    'message' => array( 'request_from' => '7' )
));

I'm not very used to php, but I hope you understand what I mean.

On the Client page you can just register to your channel ('friend_request_') and then handle the requests:

// PUBNUB.subscribe() - LISTEN
PUBNUB.subscribe({
    channel  : "friend_request_<? echo $user_ID; ?>",
    callback : function(message) { alert('FRIEND REQUEST FROM USER ID: ' + message.request_from) }
})

So with this solution you will not have to handle any timings or loops, because pubnub handles this for you. Facebook does this (as far as I know) and BeaconPush is used by EA for the Battlelog, which, in my opinion, is a great website with a lot of interessting web techniques.

Post a Comment for "Automatic Notification In Database Change :similar With Facebook Friend Request"