im developing a texting feature that will get the most recent messages from the database every couple of seconds, essentially this process does a continuing database search to be able to display new messages.

illustration of the code:

 function getLatestActivities(){
    var ignoreMessagesArr = $("input.activityId").map(function(){ return this.value; }).get().join(",");
    var profileId = $("input#userActivityId").val();

    $.ajax({
        traditional: true,
        dataType: "json",
        type: "GET", url: "include/process.php", 
        data:{
            getLatestActivity: "true",
            toUser: profileId,
            ignoreMessages: ignoreMessagesArr
        },
        success: function(data){
            $.each(data, function (i, elem) {
                $('.commentMessage').after(elem.value);
            });              
        }
    });   
}

What I wish to know is whether or not there's a far more efficient method of carrying out this, i.e. discovering a general change in the database rather(???).

You might like to consider Socket.IO it's targeted toward realtime communications using the server. You'll be able to design the after sales to push data towards the client when it is available instead of constantly polling the database for brand new information in the frontend

HTML5 Web Sockets enables for 2 way communication between your client as well as your server...

It may sound as if you would like your web application to get data if this alterations in the database, although not always to transmit data instantly. If two-way communication is needed then you're searching for Web Electrical sockets which Socket.IO can help you with server side but mandates that you take a Node server. There's a Google code project that allows Web Electrical sockets for PHP known as PHPWebSocket but, basically remember right, mandates that you take it inside a separate process (i.e. run it in the command line). To ensure that type of takes proper care of the server part, however you need to bother about the leading-finish.

Presently only Opera and Chrome fully support Web Electrical sockets according to CanIUse. For individuals browsers missing support you'll need a polyfill. Here is a list of HTML5 polyfills. So Web Electrical sockets could be kind of chaos to implement, so make certain that is what for you to do.

However in case your webapp only must receive data then EventSource (a.k.a. Server Sent Occasions) is what you want. The support is better around the front-finish and also you don't need to do much around the server. Again, for under stellar browsers you may need a polyfill, but that virtually means IE. Begin to see the following sites/lessons regarding how to make use of this feature.

If none of this works you will find a couple of options. Constant polling with a couple type of repeating structure like setTimeout, lengthy polling (a.k.a. hanging get) in which the server leaves the AJAX request open until there's new data, and there is even the infinite iframe trick, and maybe even a Expensive wordpress plugin that connects towards the server to obtain data.

For me you need to request just the last ID placed inside your database and add it as being a parameter to for your ajax request.

process.php should handle that ID and when you will find other rows to complete the search.

like

$query = mysql_query("SELECT `ID` FROM `table` WHERE `ID`>'$lastId'");
$result = mysql_num_rows(); //use that to see if you have new rows.