Reputation: 14233
Can anybody tell how to retrieve data from database like that of Facebook's notification . my requirement i have a user table and a message table when ever a user need to send a message an entry is made in the message table with sender and recipient user id. so i need to show a new incoming message status to the recipient in real time.
Upvotes: 4
Views: 3412
Reputation: 738
you can use the AJAX for the particular portion of the table for regular checking of new updates. but this will consume the max resources of the server so for that solution is to use a different server and make a cron job with the AJAX for regular updates.
If you say, just like chat it would be better to go with the APE as it goes as per your requirements.
Upvotes: 0
Reputation: 1382
The technology is called Comet. The idea is that JavaScript makes AJAX call to the server. Server accepts connection and does not return anything until it found something for client. Imagine if I want to get instant notifications, my browser makes AJAX call to server, server is looking for new notification in database. If there are any it immediately returns them, if not it just sleeps for example 0.5 second, then query database again until it found something new there.
Here example code of making Comet:
<?
if(!isset($_POST['notificationLastId'])){
exit;
}
// Query database every 0.5 seconds
$interval = 0.5;
// Hold maximum 30 seconds
$timeout = 30;
// You need to close session if it open otherwise all other queries within
// same session will hang, because of session locking
session_write_close();
$startTime = time();
$response = array();
while (true){
// Query database to see if there are any new
// notifications with id greater then $_POST['notificationLastId']
if(count($newNotifications) or time()-$startTime > $timeout){
$response['newNotifications'] = $newNotifications;
// get last id of notifications table
$response['lastId'] = $notificationLastId
header('Content-type: application/json');
echo json_encode($response);
}
// Sleep not to cosume too much server resources
usleep($interval*1000000);
}
exit;
?>
Upvotes: 1
Reputation: 5931
It's a lot like ajax, except the goal is different: more open sockets hanging and less requests versus more requests and less open sockets. The JS makes a request to the server ( 2 of them actually ) and the server doesn't respond immediately, it instead hangs and sleeps until there is actually data to send to the client in which case it returns the content immediately. The reason for having 2 open hanging requests is that once one of them times out the second one is still ready to receive notifications while the 1st one reconnects. This way there is always at least one hanging request. Could check out APE, or COMET, or "reverse ajax", or "long polling with javascript". There are benefits and disadvantages to using long polling instead of classical polling, I would suggest you look into which one better fits your scenario.
Cheers
Upvotes: 3
Reputation: 1247
What I know is that Facebook does something with the mousemove event in javascript in combination with a timeout. If you don't move you cursor, the event of getting the notifications is not triggered. When you move your cursor and there is no timeout specified then the event of getting the notifications is triggered and a new timeout has been sent. If this new timeout expires the event is triggered and if the cursor hasn't been moved no new timeout will be set.
To get the notification, use ajax to get the unread messages for the message table.
Upvotes: 1