Reputation: 6488
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 the 'add friend' link on user B's profile, friend-request records will be made in A's and B's databases accordingly. When B visits the waiting friend-requests- showing-page ( as such the profile page), the request will be shown querying B's db.This much is pretty simple to do i think.
But while B is online, C can make a friend-request to B. I want to make a notification to B that C has made such a request even if B does not refresh his/her profile page(or any page with the option of showing the waiting friend-requests ). As to the type of notification, it can be a box showing the total number of friend-requests waiting. Clicking on the box will show the details. Or it could be in any other form.
My point of interest is how to make B aware of a new friend request while B is online without making him/her refresh the page containing the friend-requests?
Upvotes: 4
Views: 12139
Reputation: 7517
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.
You can do two types of polling :-
Next, you can implement any of these in two different ways:-
I hope that this makes a clear idea to you!
Upvotes: 9
Reputation: 1460
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 send
cache: 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.
Upvotes: 4
Reputation: 481
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:
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.
Upvotes: 1
Reputation: 7517
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:
"SELECT COUNT(*) FROM table_friendship WHERE friend_to = B AND approved = "NO"
(Just a pseudo-SQL only!)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)
Upvotes: 2