Event Source On Labs

1,672 views

Published on

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.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,672
On SlideShare
0
From Embeds
0
Number of Embeds
76
Actions
Shares
0
Downloads
5
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Event Source On Labs

  1. 1. EventSource emulation A labs.ericsson.com enabler https://labs.ericsson.com/apis/eventsource/
  2. 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. 2
  3. 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. 3
  4. 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/ 4
  5. 5. EventSource server example A simple PHP script can be used to serve the client with an appropriate event-stream: <?php header("Content-Type:text/event-stream"); while(true) { echo "event: server-timen"; $time = time(); echo "data: $timen"; echo "n"; flush(); sleep(3); } ?> 5
  6. 6. 6

×