1

TO THE PEOPLE WHO VOTED THIS NOT A REAL QUESTION THAT IS ABSOLUTELY IDIOTIC.

I have searched and searched with Google over a month now and I can't find an accurate example that actually works of ajax long polling with php and mysql. Preferably would like to find an example ajax long polling with mysql and codeigniter.

Anyone comes across this question and has a good example please let me know.

Anyone who reads this that thinks they know the ajax long polling I'm looking for please email me or let me know on here. I have chat app almost done utilizing Codeigniter but I am having trouble with jquery/ajax long polling part that is client side. I have not posted this chat app because last time I posted another chat app I have another developer on here complained there was too much code I posted. I am ready to send this code to anyone who can actually provide ajax long polling code that works.

Thanks so much.

eNigma
  • 101
  • 4
  • 10
  • I would use web sockets for this as ajax is a poor choice. This looks to have answered you question tho see - http://stackoverflow.com/questions/333664/simple-long-polling-example-code – daxroc Oct 18 '12 at 00:30
  • 2
    Hey daxroc. Yes came across websockets example but none have worked. Plus websockets are not compatible with every browser because this type of esoteric programming is fairly new compared to ajax long-polling which I am not saying is the most robust method. Also I am looking at comet as another solution. You can tell me what you think. Also wanted to thank you for help. Right now I need code though that works and gets the job done. If you know an excellent come example that would work. Please let me know. Thanks also for not voting down my question or closing as not a real question. – eNigma Oct 18 '12 at 21:09

1 Answers1

5

I suppose that you store the chat messages in a db?? so one a approach will look like this:

the most important thing is that you need to do at the very first time is deliver to the user the server time, this is the key to get the new chat messages, so first, we do this:

var time;
$.ajax( {
     url: 'http://yoursite.com/chat/get_time',
     success: function( dataReponse ) {
         time = dataResponse;
    },
    type: 'GET'
} );

based on the url "http://yoursite.com/chat/get_time", you need a controller named "chat" with a function with the name of "get_time", this function need to response the server time in milliseconds, so we do this:

function get_time() {
    echo time();
}

now we start to polling to the server new chat messages, we do this:

function getNewMsgs() {
    $.ajax( {
        url: 'http://yoursite.com/chat/get_new_msgs',
        type: 'POST',
        // send the time
        data: { time: time },
        success: function( dataResponse ) {
            try {
                dataResponse = JSON.parse( dataResponse );
                // update the time
                time = dataResponse.time;
                // show the new messages
                dataResponse.msgs.forEach( function( msg ) {
                    console.log( msg );
                } );
                        // repeat
                        setTimeout( function() {
                             getNewMsgs();
                        }, 1000 );
            } catch( e ) {
                // may fail is the connection is lost/timeout for example, so dataResponse
                // is not a valid json string, in this situation you can start this process again
            }
        }
    } );
}

comebacj to "chat" controller, we need to code the "get_new_msgs" function:

function get_new_msgs() {

    $this->load->model( 'chat_msg' );
    echo json_encode( array(
        'msgs' => $this->chat_msg->start_polling(),
        // response again the server time to update the "js time variable"
        'time' => time() 
    ) );
}

in "chat_msg" model, we code the "start_polling" function:

function start_polling() {
    // get the time
    $time = $this->input->post( 'time' );
    // some crappy validation
    if( !is_numeric( $time ) ) {
        return array();
    }

    $time = getdate( $time );
    // -> 2010-10-01
    $time = $time['year'] '-' + $time['mon'] + '-' + $time['mday'];

    while( true ) {

        $this->db->select( 'msg' );
        $this->db->from( 'chat_msg' );
        $this->db->where( 'time >=', $time );
        $this->db->order_by( 'posted_time', 'desc' );
        $query = $this->db->get();

        if( $query->num_rows() > 0 ) {
            $msgs = array();
            foreach( $query->result() as $row ) {
                $msgs[] = $row['msg'];
            }
            return $msgs;
        }

        sleep( 1 );
    }
}

get warning, I wrote this code in my mind, I do not have a web server to my disposition at this moment to test this code.

nobody
  • 98
  • 3