Reverse Ajax implementation using php
Asked Answered
I

4

6

I am looking to implement reverse ajax in my application which is using PHP and jquery. I have googled a bit about it and found XAJA but that seems to be a paid application. Is there an open source application available for the same or has someone implemented it?

Some pointers or hints would be very helpful.

Thanks in advance.

Ironing answered 31/12, 2010 at 6:26 Comment(3)
You mean something like Comet, or HTML5-style websockets?Flagrant
I have read about Comet but that needs intensive support from server side and as I have found, it does not perform well with apache server.Ironing
HTML5-style websockets seems to be interesting but needs support from browsers. As per the document currently only safari and chrome support it?Ironing
C
1

I know of two types of reverse AJAX:
1- Polling
2- Pushing

I think polling is rather easier to implement, you just have your javascript make a regular request to the server every time interval, and when the server have some data for it it will respond. Its like a ping and some call it heartbeat, but its the very obvious solution for this problem. However it may easily overload the server.

EDIT Simple polling Example code:
Server-Side:

<?php
//pong.php php isn't my main thing but tried my best!
$obj = new WhatsNew();
$out = "";
if ($obj->getGotNew()){
    $types = new array();
    foreach ($obj->newStuff() as $type)
        {
            $new = array('type' => $type);
            $types[] = $new;
        }

    $out = json_encode($types);
}
else{
    $out = json_encode(array('nothingNew' => true));
}


Client-Side:

function ping(){
    $.ajax(
        {

            url : "pong.php",
            success : function (data){
                data = JSON.parse(data),
                if (data['nothingNew'])
                    return;
                for(var i in data){
                    var type = data[i]['type'];
                    if (type && incomingDataHandlers[type]){
                        incomingDataHandlers[type]();
                    }
                }


        });
}
incomingDataHandlers = {
    comments: function () {
        $.ajax({
            url: "getComments.php",
            method: "GET",
            data: getNewCommentRequsetData() // pass data to the server;
            success : function (data){
                //do something with your new comments
            }
        });
    },
    message: function (){
        $.ajax({
            url: "getMessages.php",
            method: "GET",
            data: getNewMessageRequestData() // pass data to the server;
            success : function (data){
                //do something with your new messages
            }
        });
    }
}
$(docment).ready(function () {
    setInterval(ping, 1000);
})
Coons answered 31/12, 2010 at 6:44 Comment(2)
Thanks amjad, I know about these types. I am looking for some hint or pointers on how it can be implemented actually.Ironing
I have seen your code but that does not seem to be an example of polling or pushing. It a simple example of ajax request repeated after a fixed interval.Ironing
F
1

You are looking for what they call "long poll" - I did a "long poll php" and I got this thread on stack overflow:

How do I implement basic "Long Polling"?

Fairminded answered 31/12, 2010 at 9:29 Comment(2)
thanks knubo, looks good. However, I have got another good link. Check this ape-project.org/home.html may be it helps you in future.Ironing
I will surely try your solution as well.Ironing
L
0

you could websockets in conjuction with "flash" websockets because almost all browser have flash on board(average around 96%? => http://www.statowl.com/flash.php) => https://github.com/gimite/web-socket-js. You could use this together with http://code.google.com/p/phpwebsocket/. Still I am wondering if the performance is going to be any good. If it all possible I would use node.js to do reverse ajax. http://socket.io is a really cool project to do this!

Luting answered 31/12, 2010 at 18:57 Comment(0)
C
0

Have you checked APE ?

Its a push based real-time data streaming technology over a single low volume ajax connection. The concept is useful, you may be able to replicate the same thing with your server-side implementation

Cherin answered 13/3, 2012 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.