This is the bocoup web log with posts from Al, Boaz, Rick, Sam, Nate & Nick. You should also make sure to checkout code.bocoup.com, where we keep the finished versions of ideas we kick around here.


May 26, 2010

Chrome 6: Server Push Events with new EventSource()

This is mighty awesome. I’ve put together some test demos, to run them you’ll first need to get this Chromium build:


Then download these:

http://gist.github.com/415116




For something (only slightly) more involved

http://gist.github.com/415294




Both examples can be found here:
http://code.bocoup.com/event-source/event-source-1.html
http://code.bocoup.com/event-source/event-source-2.html

They have only been tested in a Chromium nightly and definitely don’t work in FireFox. Be sure to have the javascript console open when running these in order to see the output.


EDIT

In the comments below, there is some interesting info about how this is actually implemented, most notably the comment left by obigid and my reply to them.


More Findings, May 27, 2010

In case you’re wondering what kind of HTTP request is being made, it is a GET request. Here are the passing tests:

http://gist.github.com/415836
http://gist.github.com/gists/415836/download



Comments:

8 Comments

  1. obigid
    Posted May 27, 2010 at 4:40 am | Permalink

    I don’t think this is “pushing” events from the server. Based on this comment:

    // despite not having the while(true){}
    // this seems to repeat pushing messages to the client

    I guess Chrome is polling the server over and over again, so this is simply a ‘shortcut’ for something every browser with XHR support can do.

  2. Thomas Broyer
    Posted May 27, 2010 at 5:25 am | Permalink

    You don’t need a nightly build, both examples work in Chrome 6.0.408.1 dev!
    Awesome!

  3. Rick Waldron
    Posted May 27, 2010 at 6:54 am | Permalink

    Glad you mentioned this… I came to the same conclusion after discovering that it would repeatedly fire the MessageEvent whether or not the PHP was on a loop. So basically, it’s faking server push by polling natively.

  4. Rick Waldron
    Posted May 27, 2010 at 6:56 am | Permalink

    Thanks! I had stumbled upon this when I was investigating Native Client and had downloaded the Chromium 6 builds that were listed in their “Getting Started” docs, so to play it safe, I only recommended the build I was certain it would work in – thanks for the info!

  5. Witek Baryluk
    Posted May 28, 2010 at 3:30 pm | Permalink

    I have two questsions. If you will add while(true) { sleep(10); … } at server side will it still work? Will other connection (AJAX) to the same server not block? Will this be asynchronously piped back to JS on the fly? Can we easly have many (50) EventSources and still this will be efficient? Can we have EventSource to other domains? Is Cross-Origin mechanisms working then?

    Sorry for questsion, but I don’t have Chrome here working. And I think this information will be usefull for everyone. :)

  6. Witek Baryluk
    Posted May 28, 2010 at 3:33 pm | Permalink

    Ah and last think, I think you should first detect if EventSource can be created (using IF or TRY/CATCH), so will detect webbrowsers which doesn’t support it).

  7. Benji Shults
    Posted June 1, 2010 at 7:34 am | Permalink

    Isn’t it a problem that they are polling the server? I mean, what happens if the server really were doing what it was supposed to do? Possible problems:

    1. Excess traffic
    2. Events sent from the server (not in response to the invisible polling) go where?

    The standard definitely says “push”. Should we assume that Chrome’s current behavior will change to match the standard some time in the future?

  8. Posted June 1, 2010 at 11:27 am | Permalink

    I don’t think this is “pushing” events from the server. Based on this comment:

    // despite not having the while(true){}
    // this seems to repeat pushing messages to the client

    I guess Chrome is polling the server over and over again, so this is simply a ‘shortcut’ for something every browser with XHR support can do.

2 Trackbacks

  1. [...] he uncovers new support for the EventSource API that allows for simple server push of DOM events as shown in this simple [...]

  2. [...] This post was mentioned on Twitter by rick waldron and kernowsoul, Fabio Caseri. Fabio Caseri said: Chrome 6: Server Push Events with new EventSource() http://bit.ly/akZgl8 [...]




Please send your questions to this address or call Bocoup at 617-379-2752
This web page is proudly maintained by Bocoup and hosted by (mt) Media Temple
All code on this website is Open Source