Short polling consists of an application requesting data from the same endpoint at a specified interval (i.e. request any new comments on this github issues every 10 seconds), as opposed to long polling, which consists of making a request, and the backend waiting to complete that request until there's data to send. (i.e. request new comments on a github issue, wait until comments are returned, then open a new request).
With RxJS, short polling is relatively easy to do, if one understands semantics of Rx well enough.
Example (Some Http APIs shown here are not yet implemented):
var lastCommentsUpdatedTime = '';
var requestOptions = new RequestOptions({
url: 'https://api.github.com/repos/angular/angular/issues/123/comments',
params: {
sort: 'updated',
since: lastCommentsTime
}
});
// Create an observable that emits a new value every 5 seconds
Rx.Observable.interval(5000)
//Create new http request, automatically cancel any pending requests.
.flatMapLatest((_) => {
var request = new Request(requestOptions.merge({
params: {
since: lastCommentsTime
}
}));
//Return Observable
return http.request(request);
})
//Get the response body
.map(res => res.json())
//Set the timestamp of last comments updated
.do(comments => this.lastCommentsUpdatedTime = comments && comments.length ? comments[0].updatedAt : this.lastCommentsUpdatedTime)
//Give the new or updated comments to the view to handle displaying the comments.
.subscribe(comments => this.appendOrMergeComments(comments));
The only part about this that I think requires deeper Rx understanding is the creation of the interval observable and piping it into the function to generate the request. Perhaps this could be made simpler, and could also be made to provide similar functionality to Dart Streams.
In addition to providing framework help, there should be good docs on how to properly do polling with Http.
Short polling consists of an application requesting data from the same endpoint at a specified interval (i.e. request any new comments on this github issues every 10 seconds), as opposed to long polling, which consists of making a request, and the backend waiting to complete that request until there's data to send. (i.e. request new comments on a github issue, wait until comments are returned, then open a new request).
With RxJS, short polling is relatively easy to do, if one understands semantics of Rx well enough.
Example (Some Http APIs shown here are not yet implemented):
The only part about this that I think requires deeper Rx understanding is the creation of the interval observable and piping it into the function to generate the request. Perhaps this could be made simpler, and could also be made to provide similar functionality to Dart Streams.
In addition to providing framework help, there should be good docs on how to properly do polling with
Http.