none
How to implement polling using Observables?

    Question

  • I have parametrized rest call that should be executed each five second with different params:

    Observable<TResult> restCall = api.method1(param1);

    I need to create Observable<TResult> polling that will execute restCall each 5 second with different param1. If api call fails i need to get error and make next call in 5 seconds. Interval between calls should be measured only when restCall is finished.

    Wednesday, October 23, 2013 4:58 PM

All replies

  • Use Observable.Create

    Following code is just to show you the concept, not tested

    IObservable<MyObject> myPollingObservable = Observable.Create<MyObject>((observer) => { CancellationTokenSource cancelSource = new CancellationTokenSource(); Task.Factory.StartNew(() => {

    while(!cancelSource.Token.CancellationRequested)

    { //call your rest service here Thread.Sleep(5000); // 5 seconds wait

    var updates = restApi.GetUpdates(); if (updateTriggered) { observer.OnNext(new MyObject() {}); }

    } }, cancelSource.Token); return Disposable.Create(cancelSource.Token); });



    Fahad


    • Edited by Fahad349 Sunday, October 27, 2013 9:51 PM
    Sunday, October 27, 2013 9:50 PM