NativeScript / nativescript-background-http

Background Upload plugin for the NativeScript framework
Apache License 2.0
101 stars 50 forks source link

Set Timeout for watting response #198

Closed lieutandat closed 5 years ago

lieutandat commented 5 years ago

Hi, I am working with nativescript for both iOS and Android, I tried to set timeout in request with your plugin, however it seem to be not apply at all. I got response code -1 after finish upload file to server few seconds while my server still running process ( will take about 5-6 minutes every request for return response). Could you help me this problem ? Thanks!

tgpetrov commented 5 years ago

Hi @lieutandat, Can you provide some more information about the desired outcome and also how to reproduce your issue. It will be best if you manage to reproduce it in one of the plugin's demo apps (demo, demo-angular or demo-vue) and let us know the exact steps to follow.

lieutandat commented 5 years ago

@tgpetrov , Server (Java) receive request api of my team is not public right now, so I can not reproduce backend api. My frontend

const url = `${path}${uri}/${userId}`;
        var request = {
            url: url,
            method: "POST",
            timeout: 3600000,
            headers: {
                "Content-Type": "application/octet-stream"
            },
            description: "Uploading "
        };
        var params = [ ];
         imagesPaths.forEach(item => {
             params.push({
                 name: item.type, filename: item.imagePath, mimeType: "image/png"
             })
         })
         params.push( { name: "identityDataRequest", value: JSON.stringify(object) })

         return session.multipartUpload(params, request);

    }

mybackend (just sample in nodejs, not real java server, it might not work the same as java)

 app.post('/identity', (req, res) => {
        console.log(req.body)
        setTimeout(() => {
            res.status(200).send({})
        }, 1000*60);
    });

http.listen('8080', () => {
  console.log('We are live on  8080');
});
http.timeout = 1000* 60 *6;
http.keepAliveTimeout =  1000* 60 *6;

when the request completed upload, my server keep request running, but session still return with event errorHandler few seconds (about 15-20s) after upload file completed. I read your plugin code that its does not support timeout attribute. Could you help me to resolve this problem ?

tgpetrov commented 5 years ago

Hi @lieutandat, Yes, you are right, nativescript-background-http plugins currently doesn't support setting timeout. The android-upload-service we are using on android has its default timeouts set here, but there is a public HTTP_STACK that can be used to set another HurlStack with modified timeouts. On iOS, we using a NSMutableURLRequest that has a timeoutInterval which defaults to 60 seconds.

lieutandat commented 5 years ago

yep @tgpetrov , I trying with added

net.gotev.uploadservice.UploadService.HTTP_STACK = new  net.gotev.uploadservice.http.impl.HurlStack(true, false, 60000, 60000);

in setRequestOptions function and it run perfect. I will try to change it on iOS and let you know when It successfull. Thank you very much!

lieutandat commented 5 years ago

Hi @tgpetrov , how can I set retries max times for ios ?

tgpetrov commented 5 years ago

@lieutandat Actually, the 60 seconds timeout that I mentioned earlier seems to be coming from the NSURLSessionConfiguration that we are creating here. The iOS documentation notes here that any task created with this configuration are automatically retried when the request fails due to timeout. There is also a timeoutIntervalForResource, which allows for limiting the time for a specific resource. It seems like combining proper values of these two properties can allow you to limit the number of retries.

lieutandat commented 5 years ago

Sorry about late reply @tgpetrov, I follow as you mention and it work fine. Thanks you very much!

acharyaks90 commented 5 years ago

yep @tgpetrov , I trying with added net.gotev.uploadservice.UploadService.HTTP_STACK = new net.gotev.uploadservice.http.impl.HurlStack(true, false, 60000, 60000); in setRequestOptions function and it run perfect. I will try to change it on iOS and let you know when It successfull. Thank you very much!

Hi @lieutandat where you have added this in plugin code or in nativescript javascript ( own service or component angular).

lieutandat commented 5 years ago

@acharyaks90 I had to change in plugin code ( setRequestOptions function). This may breach licence law, I do not have more knowledge with Apache license, Does it allow to change plugin source code ?

acarlstein commented 4 months ago

Will you help me? I'm trying to build two applications.

One application requires to create a series of timers. The other application requires communication with a server to exchange coordinates.

Both applications must work when the app goes in the background, when the user locks the screen, or when the screen goes black.

Could someone please confirm whether setTimeout and setInterval work in the background for iOS and Android?

I'm aware that android might required a permission to be added into the manifest to do so. I know that iOS requires adding keys/values in the Info.list to do so.

If they do but require extra work, could a link to a tutorial be included? Thank you