Simple Comet Example Using PHP and Jquery

jQuery and PHP - Comet

Can I Use on Web Sockets - Working Draft:


Partial support refers to the websockets implementation using an older
version of the protocol and/or the implementation being disabled by
default (due to security issues with the older protocol). Microsoft is
currently experimenting with the technology.

Sample Image

http://i.imgur.com/20X5z.png

The technology just isn't there yet. Watch this video from SymfonyLive if you want a better grasp on what the specs for HTTP and REST mean. It's interesting, and apparently Twitter goofed.

Also see:

http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations

PHP AJAX Comet with MySQL select record

Yes you can use ajax for this and simply update a div in your html.
You need to have jquery linked in order to use the below code.

show_log(){
var lnk = "link to the viewlog.php file";
$.ajax({url:lnk,success:function(result){
$("#log_div").html(result);
}});
}

Run the show_log() function every x number of mins.
Have your viewlog.php show the last x number of records in the descending order of time.
You can update your sql to look like

$sql = "select log_description,log_time from log ORDER by log_time DESC LIMIT 5 ";

You can use the below inside your javascript to run the function every x number of seconds. In this every 10 seconds.

window.setInterval(function(){
show_log();
}, 10000);

the 10,000 is in miliseconds

----- Try the below

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<script>
http = getHTTPObject();

function getHTTPObject(){
var xmlhttp;

if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
try {
xmlhttp = new XMLHttpRequest();
}catch(e){
xmlhttp = false;
}
}
return xmlhttp;
}
function show_log(){
var url = "viewlog.php";
http.open("GET", url, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
function handleHttpResponse(){
if(http.readyState == 4){
document.getElementById('log_div').innerHTML = http.responseText;
}
}
setInterval ( "show_log()", 5000 );
</script>

</head>
<body>
<div id="log_div"></div>
</body>
</html>

Are there any jQuery functions or plugins for comet on apache?

If you are only doing long polling then jQuery will work fine. However, jQuery does not expose a readyState === 3 event, so there is no built in way to get data as it is streaming if that is the direction you want to go.

[Edit]
Here is the bug, #1172

And it looks like they added the functionality in 1.5, using a Prefilter

So yes, you can do all the comet stuff with jQuery now :)

PHP - jQuery - Comet chat

In short, I would tell you to use Node.js as your backend technology.

Here's a more lengthy explanation: Creating a live checkers-like web app with PHP, JS, CSS and HTML?

using comet iframe with jquery?

A cool link is here: Comet and jQuery

Nginx with the NHPM module + jQuery + PHP

how does comet work with php?

use this link:

http://www.zeitoun.net/articles/comet_and_php/start

That is the best tutorial i could found, and takes 1 min to try;

in short:

alt text

( image from that tutorial )

index, can be html or php, creates a request, which php doesnt answer until there is data to send back, with chat, when someone sends you a message.

If you have many users chatting, i recommend using a java chat app

otherwise your server will load up with running php engines ( each unanswered request keeps a php engine alive, which is server capacity ).

http://streamhub.blogspot.com/2009/07/tutorial-building-comet-chat.html

this should help you out with that, but you do need java hosting :)

have fun

edit:

just read the other server part; sending requests to your own server can get messed because the timeout function may not work well, so the server crashes, an independant server timeouts the connection after a certain amount of time, no matter what.



Related Topics



Leave a reply



Submit