Event Source On Labs

6
EventSource emulation A labs.ericsson.com enabler https://labs.ericsson.com/apis/eventsource/

description

Write future-proof Web Applications using server-sent events. The EventSource enabler provides a lightweight javascript library that allows you to use the current W3C Event Source draft API with almost any web browser.

Transcript of Event Source On Labs

Page 1: Event Source On Labs

EventSource emulation

A labs.ericsson.com enablerhttps://labs.ericsson.com/apis/eventsource/

Page 2: Event Source On Labs

2

EventSource emulation

EventSource is an API for receiving push messages “server-sent events” that is currently being standardized and implemented natively in browsers.

The EventSource emulation enabler allows you to use the EventSource API already now in browsers that do not yet support server-sent events.

Additionally, the enabler proxy allows you to set up your own server that provides a regular event-stream that will work with a native implementation of EventSource.

Page 3: Event Source On Labs

3

Why EventSource emulation?

The EventSource emulation enabler provides a simple API for receiving pushed messages to web applications that you can use already today.

Once more browsers implements the EventSource functionality natively, your web application will then start using the native implementation automatically.

Page 4: Event Source On Labs

4

EventSource client example

To use the enabler, simply include the enabler JavaScript as such:<script src="http://eventsource.labs.ericsson.net/ESProxy/library/?key=YOUR_API_KEY"></script>

Use the EventSource constructor to start receiving events from your server:var es = new EventSource(“http://example.org/event-stream”);

Add an event listener to the EventSource object to handle incoming events:es.addEventListener(“server-time”, function (evt) { alert(“time on server is:” + evt.data); }, false);

The EventSource specification is available here:http://dev.w3.org/html5/eventsource/

Page 5: Event Source On Labs

5

EventSource server example

A simple PHP script can be used to serve the client with an appropriate event-stream:<?phpheader("Content-Type:text/event-stream");while(true) {

echo "event: server-time\n";$time = time();echo "data: $time\n";echo "\n";flush();sleep(3);

}?>

Page 6: Event Source On Labs

6