Topic: [alpha] Comet streaming

Hi,

Since we've had alot request in our forums regarding comet streaming I decided to write a plugin for xajax 0.5 beta4. At the moment, it's still very experimental - that's why I name it "alpha". It has been tested in FireFox 2, Safari 3 (osx leopard), Opera 9+ and IE 6/7.

Example: http://xajaxproject.org/developer/q_no/Comet/comet.php
Download: http://xajaxproject.org/developer/q_no/Comet/Comet.zip

The zip file contains the latest xajax 0.5 SVN version. I added a header attribute to xajax.request(). Using this attribute you can pass all kind of informations per HTTP-Header. The streaming plugin uses this attribute to switch the transport method on serverside. Since the IE doesn't support multipart XHRs I've had to create a fallback method using a hidden iframe (no polling!).

The setup is quite easy and should be self explanatory when you look into the comet.php file.

Paths / includes

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

$xajax = new xajax();
require_once $core . '/xajaxPlugin.inc.php';
require_once $core . '/xajaxPluginManager.inc.php';
require_once './xajax/xajax_plugins/response/comet/comet.inc.php';

Configure the JavaScript URI

$xajax->configure('javascript URI', '/developer/q_no/Comet/xajax/');

Take care, that you configure the JS URI after including the plugin. Otherwise xajax will not pass the javascript uri to the plugin and comet request might hook up the browser. (I've crashed my FF many times while developing the plugin big_smile)

registering a comet function

$xajax->register(XAJAX_COMET,"testComet");

This is very similar to registering normal functions, except that you have to set XAJAX_COMET instead of XAJAX_FUNCTION. Objects and object methods aren't yet supported.

the response function

function testComet () {
    $objResponse = new xajaxCometResponse();

    for ($i=0;$i<=50;++$i) {
        
        $objResponse->assign("progress","style.width",($i*3)."px");
        $objResponse->assign("progress","innerHTML",($i*2).'%');
        $objResponse->flush();
    }
    
    return $objResponse;
}

As you can see, I've extended the xajaxResponse class. I've added the function ->flush() to flush the response command queue. You can see the command execution immediatly in your browser after flushing the queue. This function also triggers a sleep() timeout of 1 sec to unload the server for a short while.

That's all for getting started.

I have to say thanks to BigBrownChunx for his first streaming approach in 0.5 beta2. It was a great help for me for getting started. furthermore I have to thank CtC for the rewritten plugin architecture. It's very nice smile and I hope to see more plugins from the community soon.

Please report ANY bugs, whishes, ideas - what ever you feel like - here in this thread. Please test it in all available browser and let me know if it works or not (OS / browser version).


Roadmap:
- reenable callback functions
- keepalive for longtime streams / reconnect when the connection is lost
- hunting bugs smile

Re: [alpha] Comet streaming

thanks q_no, i will test it in my developement.

feedback will come the next days...

Re: [alpha] Comet streaming

hint:

under PHP4 the comet.inc.php gives out

Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in
xajax_plugins/response/comet/comet.inc.php on line 125

replace "private" with "var" to get it work again under php4

private $bHeaderSent = false;

to

var $bHeaderSent = false;

Re: [alpha] Comet streaming

Update: added opera 9+ support smile

I have still have todo some cleanup, test parallel streams and find some bugs I didn't notice yet (I hope only a few), but so far it looks good in all tested browsers. Please give it a try and let me know if it works smile Thanks!

Re: [alpha] Comet streaming

Just to point out that php4 will no longer be supported here soon.

Re: [alpha] Comet streaming

HEY MAN,

You get my idea !!!!

http://community.xajaxproject.org/viewtopic.php?id=4046


I have this idea before...i want my credits.... hahuaauhauhau smile


Good Job...I think before but dont do nothing, only a sugestion ...

Where is my name on the plugin ?

auhhauhauahu
hehehe


Thx.

7 (edited by guilhermepl 2008-01-09 5:10:47 PM)

Re: [alpha] Comet streaming

I made the download of the comet code (comet.zip), but i got the messange "the xajax component cannot be included..."

i just put the code on my web site to test. http://thewaterinc.com/comet/comet.php . I dont change anything.

Thank you.

Re: [alpha] Comet streaming

guilhermepl wrote:

I dont change anything.

That's the point wink you have set the path using $xajax->configure("javascript URI","/path/to/xajax");

Anyway, I've spent the last 3 weeks improving the comet response handling and I'm planning to release a more stable version within the next days or week. The syntax will be the same, so you can use the current release to check out if it works for you or not wink

Re: [alpha] Comet streaming

Hi and thanks for your efforts with comet stream.

This interest me very much. What I am trying to accomplish may fit this approuch. I am interested in creating a dashboard of sorts this will update a page on a periodic basis with new information gathered from a database.

I'd like to keep the stream going until the user exits the page. I tryed this, however, it seems that I lose the stream after a few minutes. Any suggestions?

Thanks again,

Jim R.

Re: [alpha] Comet streaming

Jreda, loosing the connection can have several reasons.

1. the server may kill long processes
2. a firewall may kill long connections
3. a PHP error

to name just a few. Please check this points first.

I would like to track down the issue with you, as it may show up some points to improve the plugin. Feel free to join us on IRC, send me an E-mail or report all your issues here smile

Re: [alpha] Comet streaming

Thanks for the quick response,

I am thinking that it may be the server that is shutting down the stream. I am not (as far as I know) behind any firewalls and the stream runs for exactly 5 minutes. At this time, the code is very simple and only returns just the time as seem in the code below.

function testComet1()
    {
    $objResponse = new xajaxCometResponse();

    while (true)
        {
        $objResponse->assign("test1", "value", date("h:i:s"));
        $objResponse->flush();
        }
return $objResponse;
}

I am running apache 2.2 and PHP 5

Any thoughts?

My best regards,

Jim R.

Re: [alpha] Comet streaming

hm... do a phpinfo(); and look for the max_execution_time or similar (not sure about the spelling)

Re: [alpha] Comet streaming

Hi again,

You are right. It was the php max_execution_time. By having myFunction reset this, it runs forever.

No I have a new problem. I need to be able to stop it. I have myFunction in an endless loop testing a $_SESSION variable. I am trying to toggle this variable with a second xajax function, call it myFunction2 when a button is pressed.

What seems to be happening is that once myFunction is running I don't get any process time to see the event posted by myFunction2. I think the flush gives way to other processed but does no allow time for anything within its own process to see other events.

Any suggestions? And thanks again for your efforts and support of this.

My best regards,

Jim R.


function myFunction2()
    {
    $objResponse = new xajaxResponse();
    $_SESSION['stopped'] = true;
    }


function myFunction()
    {
    $objResponse = new xajaxCometResponse();
    $_SESSION['stopped'] = false;

while ( $_SESSION['stopped'] === false )
       {
        set_time_limit(300);
        $objResponse->assign("test1", "value", date("h:i:s"));
        $objResponse->flush();
        }
return $objResponse;
}

Re: [alpha] Comet streaming

What seems to be happening is that once myFunction is running I don't get any process time to see the event posted by myFunction2. I think the flush gives way to other processed but does no allow time for anything within its own process to see other events.

I suppose you are using file based sessions. Each process using a session locks the session file for all read/write accesses. That's why other php processes don't run the meantime. The trick is to use session_write_close() / session_start(); inside your streaming loop. while the streaming process is sleeping unlock the session file that other processes/requests can access it.

I have a quite similar solution for my irc chat with comet streaming smile

Re: [alpha] Comet streaming

q_no wrote:

What seems to be happening is that once myFunction is running I don't get any process time to see the event posted by myFunction2. I think the flush gives way to other processed but does no allow time for anything within its own process to see other events.

I suppose you are using file based sessions. Each process using a session locks the session file for all read/write accesses. That's why other php processes don't run the meantime. The trick is to use session_write_close() / session_start(); inside your streaming loop. while the streaming process is sleeping unlock the session file that other processes/requests can access it.

I have a quite similar solution for my irc chat with comet streaming smile

Just don't forget the @ when calling session_start() to avoid having the server send the warning message

@session_start()

I had this issue for my chat system ;-)

Re: [alpha] Comet streaming

the @ slows everything down. So don't use if don't need it tongue but it's correct, it might throw warnings/notices

Re: [alpha] Comet streaming

When clicking "start comet" for the 2nd time after the first one has already started, two comets run simultaneously.

18 (edited by ameseguer 2008-01-16 8:49:11 PM)

Re: [alpha] Comet streaming

Just disable the button at the beginning, call the flush function an enabled it at the end

      function testComet () {

        $objResponse = new xajaxCometResponse();
        
        $objResponse->assign( $buttonId,"disabled","true");
        $objResponse->flush();

        for ($i=0;$i<=50;++$i) {         

          $objResponse->assign("progress","style.width",($i*3)."px");

          $objResponse->assign("progress","innerHTML",($i*2).'%');

          $objResponse->flush();
        }

        $objResponse->assign( $buttonId, "disabled", "false");

        return $objResponse;
      }

Re: [alpha] Comet streaming

I tried to install Comet but it doesn't work. Is it compatible with xajax 0.5 beta 4b ? or should I install it on xajax 0.5 beta 4 ?

The only changes that I did were to replace private" with "var" to correct the error reported by evil-l33t, and change the paths. comet.php loads correctly, but nothing happens when I click on the buttons. I get no error message.

How should I debug this?

Re: [alpha] Comet streaming

aldor, the comet streaming will not work with 4b since it uses a feature that's only available in SVN atm. That's why I included a SVN vers of xajax. As soon as the RC1 release is out I'll update the scripts accordingly.

Please doublecheck that the comet.js is included correctly, otherwise it might crash your browser tongue let me know if it works

Re: [alpha] Comet streaming

OK, actually I installed your whole zip, but I just unzipped it on top of the 4b version. I'll try a clean install this WE.

You're doing a great job with this plugin... This will really enable a superior level of interactivity.

I did not look into the details of the code, are you using a general protocol such as Bayeux to implement Comet?

Re: [alpha] Comet streaming

Hi and thanks again for all the support.

Everything is working great. However, I think IE has a memory issue / leak. It seems to keep growing memory as long as the stream is running. The rate seems to be about 19KBytes per second.

Any thoughts? Is this a known issue with IE?


Thanks again,

jreda

Re: [alpha] Comet streaming

I know the issue, but unfortunately not how to solve it in a clean way atm. Since IE doesn't support XHR streaming the plugin creates an Iframe. Per $objResponse->flush() call the comet plugin fires a javascript command that passes the response queue to the response processor. So, per ->flush() call the document inside the iframe is growing.

Re: [alpha] Comet streaming

Thanks for the update. Are there any supported browsers that don't have this problem?

Thanks a lot,

jreda

Re: [alpha] Comet streaming

FireFox and Safari support XHR streaming - which work great. I still have to do some Safari vers checks, but Safari3 on OSX Leopard works like a charm. Opera supports HTML5Draft "streaming" which works similar to the IE solution, but as far as I can tell it doesn't leak so much memory over time.

I have an updated version (improved XHR streaming) nearly ready, but didn't had the time to clean up the code the last two weeks. sad