RxJS Continuous HTTP polling in case it fails as well












0















I am trying to implement HTTP-polling. The expectation is for every 5 minutes it should poll from HTTP or I could say sync on Server. In case if it fails, it shouldn't stop the subject but it should retry three times then again it should try after 5 mins. In case if it fails after 5 mins as well, it should retry 3 mins, and the scenario continuous to go on.



What I tried is something.



const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
.switchMap(() => Rx.Observable.of(axios.get(url)).retry(3));

// .map(response => console.log(`console here !! - ${response}`));
// .concatMap(val => of(`Delayed by: ${val}ms`).pipe(delay(val)));

checkTimeForRestart$.subscribe(
x => console.log(x),
error => console.error(`Error: ${error}`),
() => console.log(`Complete: fires when the observable completes`)
);


If the server is not up or for connectivity issues it shouldn't complete the observable, but should try for 3 times then again 5 mins after it would redo the process.










share|improve this question

























  • Did you try learnrxjs.io/operators/error_handling/retrywhen.html ?

    – madjaoue
    Nov 22 '18 at 9:04
















0















I am trying to implement HTTP-polling. The expectation is for every 5 minutes it should poll from HTTP or I could say sync on Server. In case if it fails, it shouldn't stop the subject but it should retry three times then again it should try after 5 mins. In case if it fails after 5 mins as well, it should retry 3 mins, and the scenario continuous to go on.



What I tried is something.



const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
.switchMap(() => Rx.Observable.of(axios.get(url)).retry(3));

// .map(response => console.log(`console here !! - ${response}`));
// .concatMap(val => of(`Delayed by: ${val}ms`).pipe(delay(val)));

checkTimeForRestart$.subscribe(
x => console.log(x),
error => console.error(`Error: ${error}`),
() => console.log(`Complete: fires when the observable completes`)
);


If the server is not up or for connectivity issues it shouldn't complete the observable, but should try for 3 times then again 5 mins after it would redo the process.










share|improve this question

























  • Did you try learnrxjs.io/operators/error_handling/retrywhen.html ?

    – madjaoue
    Nov 22 '18 at 9:04














0












0








0


1






I am trying to implement HTTP-polling. The expectation is for every 5 minutes it should poll from HTTP or I could say sync on Server. In case if it fails, it shouldn't stop the subject but it should retry three times then again it should try after 5 mins. In case if it fails after 5 mins as well, it should retry 3 mins, and the scenario continuous to go on.



What I tried is something.



const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
.switchMap(() => Rx.Observable.of(axios.get(url)).retry(3));

// .map(response => console.log(`console here !! - ${response}`));
// .concatMap(val => of(`Delayed by: ${val}ms`).pipe(delay(val)));

checkTimeForRestart$.subscribe(
x => console.log(x),
error => console.error(`Error: ${error}`),
() => console.log(`Complete: fires when the observable completes`)
);


If the server is not up or for connectivity issues it shouldn't complete the observable, but should try for 3 times then again 5 mins after it would redo the process.










share|improve this question
















I am trying to implement HTTP-polling. The expectation is for every 5 minutes it should poll from HTTP or I could say sync on Server. In case if it fails, it shouldn't stop the subject but it should retry three times then again it should try after 5 mins. In case if it fails after 5 mins as well, it should retry 3 mins, and the scenario continuous to go on.



What I tried is something.



const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
.switchMap(() => Rx.Observable.of(axios.get(url)).retry(3));

// .map(response => console.log(`console here !! - ${response}`));
// .concatMap(val => of(`Delayed by: ${val}ms`).pipe(delay(val)));

checkTimeForRestart$.subscribe(
x => console.log(x),
error => console.error(`Error: ${error}`),
() => console.log(`Complete: fires when the observable completes`)
);


If the server is not up or for connectivity issues it shouldn't complete the observable, but should try for 3 times then again 5 mins after it would redo the process.







rxjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 21:17









ischenkodv

3,28812124




3,28812124










asked Nov 22 '18 at 6:56









SankalpSankalp

34531439




34531439













  • Did you try learnrxjs.io/operators/error_handling/retrywhen.html ?

    – madjaoue
    Nov 22 '18 at 9:04



















  • Did you try learnrxjs.io/operators/error_handling/retrywhen.html ?

    – madjaoue
    Nov 22 '18 at 9:04

















Did you try learnrxjs.io/operators/error_handling/retrywhen.html ?

– madjaoue
Nov 22 '18 at 9:04





Did you try learnrxjs.io/operators/error_handling/retrywhen.html ?

– madjaoue
Nov 22 '18 at 9:04












1 Answer
1






active

oldest

votes


















0














So the problem is that when a it retries 3 times then the error notification is sent forward which disposes the chain and that's not what you want.



An easy solution is using catch (catchErrorin RxJS 6) to ignore the error after 3 retries:



const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
.switchMap(() => Rx.Observable.of(axios.get(url))
.retry(3)
.catch(err => Rx.Observable.empty())
);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425399%2frxjs-continuous-http-polling-in-case-it-fails-as-well%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    So the problem is that when a it retries 3 times then the error notification is sent forward which disposes the chain and that's not what you want.



    An easy solution is using catch (catchErrorin RxJS 6) to ignore the error after 3 retries:



    const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
    .switchMap(() => Rx.Observable.of(axios.get(url))
    .retry(3)
    .catch(err => Rx.Observable.empty())
    );





    share|improve this answer




























      0














      So the problem is that when a it retries 3 times then the error notification is sent forward which disposes the chain and that's not what you want.



      An easy solution is using catch (catchErrorin RxJS 6) to ignore the error after 3 retries:



      const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
      .switchMap(() => Rx.Observable.of(axios.get(url))
      .retry(3)
      .catch(err => Rx.Observable.empty())
      );





      share|improve this answer


























        0












        0








        0







        So the problem is that when a it retries 3 times then the error notification is sent forward which disposes the chain and that's not what you want.



        An easy solution is using catch (catchErrorin RxJS 6) to ignore the error after 3 retries:



        const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
        .switchMap(() => Rx.Observable.of(axios.get(url))
        .retry(3)
        .catch(err => Rx.Observable.empty())
        );





        share|improve this answer













        So the problem is that when a it retries 3 times then the error notification is sent forward which disposes the chain and that's not what you want.



        An easy solution is using catch (catchErrorin RxJS 6) to ignore the error after 3 retries:



        const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
        .switchMap(() => Rx.Observable.of(axios.get(url))
        .retry(3)
        .catch(err => Rx.Observable.empty())
        );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 9:27









        martinmartin

        42.7k1187128




        42.7k1187128






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425399%2frxjs-continuous-http-polling-in-case-it-fails-as-well%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen