Skip to content

http timeout seems not to work #6523

Closed

Description

@felixkrautschuk

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI:
  • Cross-platform modules: 4.2.1
  • Android Runtime: 4.2.0
  • iOS Runtime: -
  • Plugin(s): -

Describe the bug
I try to set a timeout for our http requests in our NativeScript app, but the timeout property has no effect for me while testing.

To Reproduce
Here is what I am doing:

httpModule.request({
        url: "http://www.mocky.io/v2/5be3174d2f00007b00ca2260?mocky-delay=10000ms",
        timeout: 5000,
        method: "GET",
        headers: { }
}).then((response) => {
        const result = response.content.toJSON();

        alert("Received response: " + response.content);
    }, (e) => {
});

The provided url leads to a sample webservice from https://www.mocky.io/ and the delay is set to 10 seconds.

Open the provided sample app and tap the button.
It will start the http request and after about 10 seconds, you get an alert with the json response.

Expected behavior
Setting the timeout to 5000 ms, I would expect, that the http request was canceled and that I would get to the failure part of the Promise.

Sample project
NSHttpTimeout.zip

Additional context
Maybe I am misunderstanding the sense of the timeout property.
The reason for me setting this property:
I want to prevent, that the user waits for a long time to get the response of an http request. Instead there should be a 'maximum' time (i.e. 10 seconds) for the user to wait and when this time has passed, the loading indicator should be hidden and it should ignore the response of this request, if it is received later.
Is this not the function of the timeout property?


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Activity

  1. self-assigned this
    on Nov 8, 2018
  2. tsonevn commented on Nov 8, 2018

    @tsonevn
    Contributor

    Hi @felix-idf,
    Thank you for the provided sample project.
    It seems that there is an issue with the HTTP module while using the timeout option. I was able to recreate the problem only on Android. On iOS, the timeout works as expected. I will mark this as a bug and would suggest keeping track on the issue for further info.

  3. Erudition commented on Aug 29, 2019

    @Erudition

    For the XMLHttpRequest, it seems that "timeout" is not even a supported event, which is causing my app to crash. I'd love to have this working!

    public addEventListener(eventName: string, handler: Function) {

    if (eventName !== "load" && eventName !== "error" && eventName !== "progress") {

    throw new Error("Event not supported: " + eventName);

  4. SpurguX commented on Nov 12, 2019

    @SpurguX
    Contributor

    I had a similar problem when I tried to set a timeout for axios requests. I used the workaround provided by Manoj in this post: https://stackoverflow.com/questions/58767691/timeout-feature-in-axios-has-no-effect-in-a-nativescript-vue-environment

  5. SpurguX commented on Nov 19, 2019

    @SpurguX
    Contributor

    Turned out that the above mentioned workaround wasn't sufficient for our requirements because only connect timeout is set for the HttpURLConnection in org.nativescript.widgets.Async's HttpRequestTask. Setting read timeout provided the desired result, that is, causing the timeout to expire when no data is received within the timeout period. So, as has also been suggested in this issue, I'd consider setting read timeout in addition to connect timeout:

    // apply timeout
    if (options.timeout > 0)
    {
      connection.setConnectTimeout(options.timeout);
      connection.setReadTimeout(options.timeout);
    }
    
  6. jarmani commented on Dec 9, 2019

    @jarmani

    @SpurguX We have gone to the same conclusion today after infinite loading on Android but not iOS. Our patch is the same as yours and fix it.

    @tsonevn What can be done to advance on this one ? This bug is pretty anoying. Thanks.

  7. added a commit that references this issue on Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions