igorw / EventSource

A PHP 5.3 library for creating an EventSource stream.
MIT License
105 stars 8 forks source link

Slow issue #1

Closed dator-zz closed 12 years ago

dator-zz commented 12 years ago

Hey !

Just try your EventSource project ! It's pretty cool !

First loading of the page is fast but if i refresh the page the page take the normal loading time + the sleep value (defined in the while loop) to load .

Is this normal ?

Here is my code (on symfony1.4)

$(function() {
    var stream = new EventSource("<?php echo url_for('@messages_poll'); ?>");

      stream.addEventListener('message', function (event) {
            console.log(event.data);
       });
});
<?php
    public function executePoll(sfWebRequest $request)
    {
        set_time_limit(0);
        ini_set('memory_limit', '512M');
        foreach (Stream::getHeaders() as $name => $value) {
            header("$name: $value");
        }

        $stream = new Stream();

        while (true) {
            $stream
                ->event()
                    ->setData('Hello')
                ->end()
                ->flush();

            sleep(2);
        }

        return sfView::NONE;
    } 
willdurand commented 12 years ago

@dator you should avoid the loop in your action. EventSource is designed to reconnect itself after a closed connection. You may tweak the retry value (in the headers, not part of the lib for now, I'm thinking about adding it) to configure your delay.

What do you think ?

igorw commented 12 years ago

I cannot reproduce the slowness with apache 2.2.20 and chrome 16. In what env is this happening?

dator-zz commented 12 years ago

@igorw Server version: Apache/2.2.20 (Unix) Chrome: 16.0.912.63 PHP 5.3.8

Maybe Symfony 1.4 ?

@willdurand I haven't dug a lot on EventSource so I cannot tell you but i found the project very interesting (HTML5 part and igor's work) .

willdurand commented 12 years ago

yep :)

Also I'll write the code of the action like that:

<?php

public function executePoll(sfWebRequest $request)
{
    set_time_limit(0);
    ini_set('memory_limit', '512M');

    $this->getResponse()->clearHttpHeaders();
    foreach (Stream::getHeaders() as $name => $value) {
        $this->getResponse()->setHttpHeader($name, $value);
    }
    $this->getResponse()->sendHttpHeaders();

    $stream = new Stream();
    $stream
        ->event()
            ->setData('Hello')
        ->end()
        ->flush();

    return sfView::NONE;
} 
igorw commented 12 years ago

@willdurand You'll have to call $response->sendHttpHeaders() before you call flush().

willdurand commented 12 years ago

Oh yes :x

dator-zz commented 12 years ago

@willdurand @igorw Nice ! No more slow :) But how can tell "Set response to client every 10 seconds ?" there is no more "Wait" part ^^?

Again thanks a lot ;)

igorw commented 12 years ago

@dator see my comments in #2, imo closing the connection every time is not really a solution.

dator-zz commented 12 years ago

you're fast ! Thanks !

igorw commented 12 years ago

So was your slowness caused by the reconnect/retry value?

dator-zz commented 12 years ago

Yep ! totally ! :)

willdurand commented 12 years ago

What should we conclude then ?

Envoyé de mon iPhone

Le 3 janv. 2012 à 00:54, Clément JOBEILIreply@reply.github.com a écrit :

Yep ! totally ! :)


Reply to this email directly or view it on GitHub: https://github.com/igorw/EventSource/issues/1#issuecomment-3335055

igorw commented 12 years ago

I'm not sure, as I was not able to reproduce the issue.