I managed to do this myself, with the only drawback being that http.get cannot be repeated more easily.
pollData(): Observable<any> { //Creating a subject var pollSubject = new Subject<any>(); //Define the Function which subscribes our pollSubject to a new http.get observable (see _pollLiveData() below) var subscribeToNewRequestObservable = () => { this._pollLiveData() .subscribe( (res) => { pollSubject.next(res) } ); }; //Subscribe our "subscription-function" to custom subject (observable) with 4000ms of delay added pollSubject.delay(4000).subscribe(subscribeToNewRequestObservable); //Call the "subscription-function" to execute the first request subscribeToNewRequestObservable(); //Return observable of our subject return pollSubject.asObservable(); } private _pollLiveData() { var url = 'http://localhost:4711/poll/'; return this._http.get(url) .map( (res) => { return res.json(); } ); };
This is why you cannot use a more direct subscription:
var subscribeToNewRequestObservable = () => { this._pollLiveData() .subscribe(pollSubject); };
Completion http.get -observable will also terminate your object and prevent it from emitting additional elements.
This is still coldly observable, so if you are not subscribed to it, requests will not be made.
this._pollService.pollData().subscribe( (res) => { this.count = res.count; } );
Spazzarticus
source share