Instant notification using php / mysql please give me a general idea

Can anyone tell me how to fetch data from a database like Facebook notification. my requirement I have a user table and a message table, when a user needs to send a message, an entry is made in the message table with the sender and recipient user id. so I need to show the status of the new incoming message to the recipient in real time.


source to share

4 answers

This is similar to ajax, except for the goal: more open sockets hang and fewer requests versus more requests and less open sockets. JS makes a request to the server (2 of them actually) and the server doesn't respond immediately, instead hangs and sleeps until the data is actually sent to the client, in which case it returns the content immediately. The reason you have 2 dangling requests open is because when one of them plays the second, it is still ready to receive notifications while one of them reconnects. Thus, there is always at least one dangling query. You can check APE, or COMET, or "reverse ajax", or "long polling with javascript". There are advantages and disadvantages of using long polls instead of classic polls.I would suggest that you investigate which one best suits your scenario.




I know Facebook is doing something with the mousemove event in javascript in conjunction with a timeout. If you do not move the cursor, the receive notification event is not fired. When you move the cursor and no timeout is specified, a receive notification event is raised and a new timeout is dispatched. If this new timeout expires, the event is fired, and if the cursor has not been moved, no new timeout will be set.

To get notified use ajax to get unread messages for message table.



The technology is called Comet. The idea is that JavaScript makes the AJAX call to the server. The server accepts the connection and returns nothing until it finds something for the client. Imagine if I want to receive instant notifications, my browser makes an AJAX call to the server, the server looks for a new notification in the database. If there are, this returns them immediately, unless it just sleeps, say 0.5 seconds, and then queries the database again until it finds something new there.

Here is some sample code for creating a Comet:


// 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

$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





you can use AJAX on a specific part of the table to check for new updates regularly. but this will consume maximum server resources, so this solution needs to use a different server and do a cron job with AJAX for regular updates.

If you talk like chatting it would be best to go with APE as it suits your requirements.



All Articles