how to get notification when a new message is recieved (like facebook)

i have a question please help me.

Let say we have a chatting room with maximum of 2 users, when one user send a message to another, the second user should be notified that new message is received just like Facebook enter image description here

I have done it with Ajax request like

    $(document).ready(
        function() {
            setInterval(function() {
                $.ajax({
                    url: 'incs/check_new_msg.php' ,
                    cache: false,
                    success: function(data)
                    {
                        $('#message').html(data);
                    },

                });
            }, 1000);
        });

<div id="message"></div>

in check_new_msg.php i use the following code

$new_msg = mysql_querey("select * from inbox where status = '0' ");
echo mysql_num_rows($new_msg);

The above code work good but the problem is that it check inbox and new message each second , but it seems harmful for processor as it run a MySQL query each second, please help me how to to execute checking query only when a new message is received.


Source: facebook

Leave a Reply