Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a "x-dom-event-stream" stream in javascript?

HTML5 draft contains an API called EventSource to stream data (notifications) trough javascript using only one server call.

Looking it up, I found an exemple on Opera Labs of the javascript part :

document.getElementsByTagName("event-source")[0]
        .addEventListener("server-time", eventHandler, false);

function eventHandler(event)
{
    // Alert time sent by the server
    alert(event.data); 
}

and the server side part :

<?php
header("Content-Type: application/x-dom-event-stream");
while(true) {
    echo "Event: server-time\n";
    $time = time();
    echo "data: $time\n";
    echo "\n";
    flush();
    sleep(3);
}
?>

But as of today, it seems only Opera has implemented the API, neither Chrome nor Safari have a working version (Am I wrong here ?)

So my question is, is there any other way in javascript, maybe more complex, to use this one stream to get data ?

EDIT : I'm looking at Comet stuff right now, but I'm not sure how to reuse that :) EDIT 2 : Apparentry, "x-dom-event-stream" has now been renamed "text/event-stream" EDIT 3 : Got to understand way more of it with this recent article from javanet

like image 313
rnaud Avatar asked Apr 01 '10 16:04

rnaud


1 Answers

See Orbited, which provides a javascript library and the server. The javascript library tries to use WebSockets and falls back to long-polling or other methods.

Here's a good guide: http://thingsilearned.com/2009/08/03/starting-out-with-comet-orbited-part-3-%E2%80%93-the-client/

like image 82
Alex Avatar answered Sep 29 '22 21:09

Alex