Topic: [beta1] Comet streaming plugin

Hi!

Finally I had some time to improve the comet streaming plugin. The syntax has slighty changed, but should be more intuitive now. The PHP and JS sources are better optimized and smaller than before. Redundant code has been removed and some bugs were also fixed.

supported browsers
- Internet Explorer 6 / 7
- FireFox 2 / 3
- Opera 8.5++
- Safari 3

demo
http://xajaxproject.org/developer/q_no/Comet/comet.php

download
http://xajaxproject.org/developer/q_no/Comet/Comet.zip
The zip archive also contains the latest XAJAX 0.5 SVN rev.

Changelog:
- removed unnecessary request plugin (XAJAX_COMET)
- supports normal xajax functions, callable objects,...
- smaller JS code
- better browser detection
- improved XHR streaming


Demo source

<?

// include xajax core
$core = './xajax/xajax_core';
require_once $core . '/xajax.inc.php';

// new xajax object
$xajax = new xajax();

// include plugin
require_once $core . '/xajaxPlugin.inc.php';
require_once $core . '/xajaxPluginManager.inc.php';
require_once './xajax/xajax_plugins/response/comet/comet.inc.php';
 
// configure JS URI
$xajax->configure("javascript URI","/developer/q_no/Comet/xajax/");

// register functions
// NOTE: the function type XAJAX_COMET has been remove
// now you can to set the 'comet mode' using the normal function attributes
$xajax->register(XAJAX_FUNCTION,"testComet",array("comet" => true));
$xajax->register(XAJAX_FUNCTION,"testNormal");

// nothing new here, process the request
$xajax->processRequest();

$_SESSION['counter'] = 0;
// the demo function    
function testComet () {
    
    // Note: In comet functions we use the xajaxCometResponse() class instead of xajaxResponse();
    $objResponse = new xajaxCometResponse();
  
  
    for ($i=0;$i<=5    ;++$i) {
        $objResponse->assign("progress","style.width",($i*3)."px");
        $objResponse->assign("progress","innerHTML",($i*2).'%');
        // flush last inserted xajax commands
        $objResponse->flush();
    }
    
    return $objResponse;
}

// a normal XHR to prove that the comet plugin didn't break anything :)
function testNormal() {
    $objResponse = new xajaxResponse();
    $objResponse->append("streaming","innerHTML","normal XHR <br />");
    return $objResponse;
}



// now the HTML PART
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>XAJAX Comet Plugin</title>
<?php $xajax->printJavaScript();?>
<link rel="stylesheet" type="text/css" href="demo.css" />
</head>
<body>

    <h1>XAJAX Comet Plugin</h1>
    <div id="docs">
    <h2>vers 0.1</h2>
        <p>
            <h3>supported configurations:</h3>
            <ul>
                <li>any webserver</li>
                <li>PHP 5.x</li>
            </ul>
            <h3>supported browsers:</h3>
            <ul>
                <li>Firefox 2/3</li>
                <li>IE 6/7 </li>
                <li>Opera 9</li>
                <li>Safari 3</li>
            </ul>        
        </p>
    </div>
    <div id="example">
        <h2>demo</h2>
        <input type="button" onclick="xajax_testComet();" value="start comet" />
        <input type="button" onclick="xajax_testNormal();" value="start normal request" />
        <div id="progress" style="width:1px;background:red;border:1x solid darkred"></div>
    <div id="streaming"></div>
        
    </div>
<br style="clear:both;" />

Re: [beta1] Comet streaming plugin

Hi, thank you for your plugin.

I managed to make it work a few weeks ago and today I wanted to add some functionality and I end up with the following error: all my comet POST responses are wrapped in a script tag

Using firebug in firefox I have the following response
<script>top.document.callback("<?xml version=\"1.0\" ?><xjx><cmd n=\"al\">Stest</cmd></xjx>");</script>

Where the real response should be
<xjx><cmd n="al">Stest</cmd></xjx>

As soon as I change the request by a classic xajaxResponse() (and removing the flush() call), it works.


Any idea where it might come from?

Re: [beta1] Comet streaming plugin

Hmm... did you use the xajax version from the Comet.zip file? In Firefox the plugin sends additional header informations and for some reason the header infos aren't sent or lost. I've updated the comet plugin to use the latest xajax SVN rev (which is close to the upcoming 0.5 RC1 release) and the header syntax has slightly changed since the last xajax rev I used in the first comet release.

Re: [beta1] Comet streaming plugin

Yes, indeed.

However, I tried with you former Comet plugin and I have the same error ... which I hadn't before. So there must be a bug in my code and it has certainly nothing to do with your new version.
I'll post it here if I can find what mistake I made...

Thanks for your (always) quick answer(s) ;-)

Re: [beta1] Comet streaming plugin

As I already said, it must have something todo with the header informations, since the plugin uses the IE fallback (xml response wrapped in JS). Please doublecheck that you'r using the newest comet+xajax rev, or post some sample code here how you call the comet functions.

Re: [beta1] Comet streaming plugin

Ok, I found out

It has something to do with my virtual hosts configuration ...
I copy pasted your example and beside the post result was exactly the same, I had no action visible on my browser. Then I disabled all my apache virtual hosts and your example worked fine.
I hadn't that problem on my production website so this has something to do with my local configuration. I'll check it out. However my former ajax requests work fine. So maybe your plugin ignore the virtual hosts redirection or something like that.

Sorry for the disturbance and thanks for the help.

Re: [beta1] Comet streaming plugin

Np smile if you have some more details tell me, perhaps it can help to futher improve the plugin wink

Re: [beta1] Comet streaming plugin

does this work in all browser's for you? You may have noticed that I didn't set exacly 1sec sleep()s for all browsers, since some behave strange when the value is raised or lowered.

Re: [beta1] Comet streaming plugin

I have a problem with the comet plugin, but I cannot figure out the reason for the problem. I have a simple script. If I use xajax without comet, everything works fine. The job of the function is to update information peridically.

Since I want to avoid polling I tried comet. But as soon as I register the comet plugin, I get an altert box, that  xajax.debug could not be loaded.  But the path is definitely right, so that's not the problem. I tried to set the script-load-timeout to 0. Then I do not get the alert, but the page still das not work. I get a javascript error then.

Fehler: xajax.debug.getExceptionText is not a function
Quelldatei: http://127.0.0.1/dal/html/dal/3rdparty/ … x_debug.js
Zeile: 91

This happens under Firefox and under IE. And it does not just happen with my application, but also with the demo shiped with the comet package. It probably has something to do with my local setup, but I cannot figure out what the problem is. I use apache from the xamp package locally under Windows XP for development.

Any ideas?

Re: [beta1] Comet streaming plugin

ancpru wrote:

Any ideas?

Unfortunately not. sad I have to admit that the comet plugin doesn't use the xajax debug functions and that I didn't test if it's compatible with the debug mode. Does it work without debugging?

Re: [beta1] Comet streaming plugin

Ahm, short question: where should I use this comet streaming? I've never heard of it...in which cases is it usefull?

Re: [beta1] Comet streaming plugin

DaveDamage wrote:

Ahm, short question: where should I use this comet streaming? I've never heard of it...in which cases is it usefull?

For example it's useful, when the client needs to be notified quickly. Instead of polling every second, which usually returns a "nothing changed", but results in heavy server load because the server has to open and close the connection every time, the client just opens a connection one time, and the server returns new data when it's available, but does *not* close the connection after sending the data.

Since i cannot get comet running in my environment now, I try to work aroudn the problem with a solution somewhere in the middle: I do a classic ajax call, but the server does not return as long there is no new data. Since I want to avoid timeouts, the server ends after 1 minute, and the client immediately polls again. This is at least a way to avoid new calls every second.

But somehow I do not like the solution because I fear some side-effects. BTW, is there a way to explecitely close all connectionns when the user leaves a page, for example something like xajax.killAllRequests();

Re: [beta1] Comet streaming plugin

q_no wrote:
ancpru wrote:

Any ideas?

Unfortunately not. sad I have to admit that the comet plugin doesn't use the xajax debug functions and that I didn't test if it's compatible with the debug mode. Does it work without debugging?

Hmm.. no. The original example (the progressbar) shipped with the comet package also does not work in my environment. I am quite sure, there is something "special" about my setup that causes the problems, but I have no idea what.

I use a Xampp package with PHP4 under Windows locally. Browser is Firefox. First I thought the quite old PHP-Version could be the problem, but since no php-errors are reported, but the problem happens inside Javascript, I doubt that this could be the problem.

14 (edited by DaveDamage 2008-03-13 9:09:57 AM)

Re: [beta1] Comet streaming plugin

I don't know if I'm right, but something's telling me that xajax or at least the commet plugin needs PHP5.
Think I have read that on the website...somewhere...somewhen :-)

Re: [beta1] Comet streaming plugin

Yep, that's correct... it would work with some small modifications (removing plublic/private declarations) but future versions of this plugin might use more PHP5 specific functions.

Re: [beta1] Comet streaming plugin

q_no wrote:

Yep, that's correct... it would work with some small modifications (removing plublic/private declarations) but future versions of this plugin might use more PHP5 specific functions.

The interesting thing in this context is, that I do not get any php-warnings or errors.

Re: [beta1] Comet streaming plugin

Hello Steffen,

at first thanks for the great plugin. I tried using it today and after a (long) time it was working ('caus I'm using 4b) and so I had to migrate my code to the working version compatible to the Comet plugin.

BUT: I guess I noticed a heavy "serialization" bug. When I try to give a content of a textarea to a function that is using the "xajaxCometResponse" and the given text value contains any slashes e.g. ' then the value isn't transferred correctly. For example: my textarea contains a string like:

UPDATE dp_images SET focal='28 mm',exposure='1/125 s',fnumber='F 2,80',model='NIKON D2Xs',iso='1',lens='28-70 mm f/2.8',sizeX='4316',sizeY='2864' WHERE filename='2008-02-24-0001.jpg';

with a "normal" xajax registerred function the full and correct value is given to the called function.
BUT: is it a Comet registerred function then the value contains only "UPDATE dp_images SET focal="

I tried using it several times and made an alert output to test it. Is it a bug of do I a mistake?

Thanks for your assistance and I looking forward to next release :-)

regards,
Lars

Re: [beta1] Comet streaming plugin

Can you please some lines of code? Especially your function call and the (comet) response function. I'll try that later when I'm at home. You can also join us on IRC (#xajax on irc.freenode.net) to discuss the issue smile

Re: [beta1] Comet streaming plugin

q_no wrote:

Can you please some lines of code? Especially your function call and the (comet) response function. I'll try that later when I'm at home. You can also join us on IRC (#xajax on irc.freenode.net) to discuss the issue smile

thanks for your answer. I have send you the used code via email.

Cheers,
Lars

Re: [beta1] Comet streaming plugin

Hi, i've tried  a new version of this comet but seems that not work with IE 6. I got error:
"context" is null or not an object.
I run the demo: http://xajaxproject.org/developer/q_no/Comet/comet.php
but also not work at my IE 6.0. Maybe have something i forget?