Issue
Let's say I have a service that makes HTTP requests to my API and returns observables - usual case.
I use that service, but the user gets impatient and decide to cancel that request, using my-subscription.unsubscribe()
. It is even possible that the request will cancel itself, using observable.timeout()
.
So the user will not receive the API's response, but it is possible that the API still received his request and treated it, without noticing him.
Is there a method in httpclient that allow me to cancel an HTTP request, but being sure that the server will not receive the request after the cancellation ?
In my case, i have a post request which may take an indeterminate amount of time. I need to be able to cancel a request BUT it's very important to not process the request if the user cancelled it. Another solution is to process the request but notify the user that the server processed it even if he unsubscribed.
Is it possible to get this behavior without using websockets ?
Solution
Is there a method in httpclient that allow me to cancel an HTTP request, but being sure that the server will not receive the request after the cancellation ?
The unsubscribe
method is the correct way to cancel a request that has already been made. More info here.
However since you say that once started the request will take an 'indeterminate amount of time' you cannot be sure that the server has already finished processing the request before it receives the request to cancel it. I honestly do not see how you can guarantee 100% NOT to process the request if the user subsequently wants to cancel for two reasons:
- You don't know how long the request will take to process
- You don't know how long the user will wait before asking to cancel.
If (2) happens after the server already finished, then your only option is the alternative you suggested, to notify the user it already processed the request.
Websockets is not going to solve this issue for you either, since it is one of timing. What you could do is put the request into a queue, so that the server does not immediately process it. You could then set a timer on the server so that it only processes requests in the queue after some fixed time (eg. 10 minutes). That would allow the user a 10 minute grace period in which they could send a cancel request, and you remove it from the queue.
Answered By - rmcsharry
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.