Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Commit

Permalink
Adding server-sent events
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpodwysocki committed Aug 15, 2014
1 parent 584e899 commit 7fa5f15
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = function (grunt) {
'src/jsonp.js',
'src/websocket.js',
'src/webworker.js',
'src/evensource.js',
'src/mutationobserver.js',
'src/requestanimationframescheduler.js',
'src/mutationobserverscheduler.js',
Expand All @@ -52,6 +53,7 @@ module.exports = function (grunt) {
'src/jsonp.js',
'src/websocket.js',
'src/webworker.js',
'src/evensource.js',
'src/mutationobserver.js',
'src/requestanimationframescheduler.js',
'src/mutationobserverscheduler.js',
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rxjs-dom",
"version": "4.0.0",
"version": "4.0.1",
"main": "dist/rx.dom.js",
"dependencies": {
"rxjs": "*"
Expand Down
59 changes: 59 additions & 0 deletions doc/operators/fromeventsource.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
### `Rx.DOM.from(url, [openObserver])`
[Ⓢ](https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/src/eventsource.js "View in source")

This method wraps an EventSource as an observable sequence which is used to send server-side events. Note that your browser must support EventSource objects.

#### Arguments
1. `url` *(String)*: The URL of the Server-Side Events.
3. `[openObserver]` *(`Rx.Observer`)*: An optional Observer to capture the open event.

#### Returns
*(`Observable`)*: An observable sequence which represents the data from a server-side event.

#### Example
```js
// Using a function for the open
var source = Rx.DOM.fromEventSource('foo.php');

source.subscribe(function (e) {
console.log('Received data: ' + e.data);
});


// Using an observer for the open
var observer = Rx.Observer.create(function (e) {
console.log('Opening');
});

// Using a function for the open
var source = Rx.DOM.fromEventSource('foo.php', observer);

socket.subscribe(function (e) {
console.log('Received data: ' + e.data);
});

socket.onNext('data');
```

### Location

File:
- [`/src/eventsource.js`](https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/src/eventsource.js)

Dist:
- [`rx.dom.js`](https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/dist/rx.dom.js) | - [`rx.dom.compat.js`](https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/dist/rx.dom.compat.js)

Prerequisites:
- If using `rx.js`
- [`rx.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/dist/rx.js) | [`rx.compat.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/dist/rx.compat.js)
- [`rx.binding.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/dist/rx.binding.js)
- [`rx.lite.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/rx.lite.js) | [`rx.lite.compat.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/rx.lite.compat.js)

NPM Packages:
- [`rx-dom`](https://preview.npmjs.com/package/rx-dom)

NuGet Packages:
- [`RxJS-Bridges-HTML`](http://www.nuget.org/packages/RxJS-Bridges-HTML/)

Unit Tests:
- [`/tests/tests.eventsource.js](https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/tests/tests.eventsource.js)
3 changes: 3 additions & 0 deletions doc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ This section contains the reference documentation for the Reactive Extensions fo
- [`Rx.DOM.post`](operators/post.md)
- [`Rx.DOM.jsonpRequest`](operators/jsonprequest.md)

Server-Sent Events
- [`Rx.DOM.fromEventSource`](operators/fromeventsource.md)

Web Sockets

- [`Rx.DOM.fromWebSocket`](operators/fromwebsocket.md)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "rx-dom",
"title": "The Reactive Extensions Bindings for the DOM.",
"description": "Library for using DOM elements as well as Ajax requests",
"version": "4.0.0",
"version": "4.0.1",
"homepage": "https://github.com/Reactive-Extensions/RxJS-DOM",
"author": {
"name": "MS Open Tech",
Expand Down
42 changes: 42 additions & 0 deletions src/eventsource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
if (!!root.EventSource) {

/**
* This method wraps an EventSource as an observable sequence.
* @param {String} url The url of the server-side script.
* @param {Observer} [openObserver] An optional observer for the 'open' event for the server side event.
* @returns {Observable} An observable sequence which represents the data from a server-side event.
*/
dom.fromEventSource = function (url, openObserver) {
return new AnonymousObservable(function (observer) {
var source = new root.EventSource(url);

function onOpen(e) {
openObserver.onNext(e);
openObserver.onCompleted();
source.removeEventListener('open', onOpen, false);
}

function onError(e) {
if (e.readyState === EventSource.CLOSED) {
observer.onCompleted();
} else {
observer.onError(e);
}
}

function onMessage(e) {
observer.onNext(e);
}

openObserver && source.addEventListener('open', onOpen, false);
source.addEventListener('error', onError, false);
source.addEventListener('message', onMessage, false);

return function () {
source.removeEventListener('error', onError, false);
source.removeEventListener('message', onMessage, false);
source.close();
};
});
};
}

0 comments on commit 7fa5f15

Please sign in to comment.