RxJS Continuous HTTP polling in case it fails as well
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
add a comment |
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
Did you try learnrxjs.io/operators/error_handling/retrywhen.html ?
– madjaoue
Nov 22 '18 at 9:04
add a comment |
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
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
rxjs
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
(catchError
in 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())
);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
(catchError
in 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())
);
add a comment |
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
(catchError
in 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())
);
add a comment |
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
(catchError
in 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())
);
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
(catchError
in 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())
);
answered Nov 22 '18 at 9:27
martinmartin
42.5k1186127
42.5k1186127
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Did you try learnrxjs.io/operators/error_handling/retrywhen.html ?
– madjaoue
Nov 22 '18 at 9:04