network.request - technically when does params.timeout fire?

Dear corona community,

I read the docs on network.request and the timeout parameter. It says:

Timeout in seconds. Default is 30 seconds.

I set the timeout to 1 second and successfully uploaded a file that took more than 1 second to upload, so my guess is that this timeout does not mean the time a connection is kept alive. What exactly does this timeout stand for instead?

Is it the time my app tries to connect to a server?

Also, which event would be fired as a result of a timeout? Would it be the event.isError?

3rd related question: Can I set a timeout after which the connection is cancelled? (lets say my network connection is pretty bad and I’d like to upload a GB of data. I’d like my app to sort of cancel this request after a while and give some sort of a popup).

I’d appreciate any information on this topic :slight_smile:

Thanks

Timeout is the time allowed for a successful handshake between a client (your app) and a server.

I might be wrong but I do not think you get a callback on a timeout but you could set a flag to false and then set it to true in your callback to let your app know it had finished a network.request() successfully.

RE no 3, you can cancel a network.request() like this https://docs.coronalabs.com/api/library/network/cancel.html

Thank you :slight_smile:

Ok, so, in other words, the timout has nothing to do with how long it may take to upload/download something AS LONG AS the first handshake took less time than whatever timeout is (default: 30 secs).

What I dont understand though: When I set the server URL to a non existing address, why does the event.isError gets fired immediately, even if timout is set to 30 seconds? How does the client know whether an URL does not exist or it does exist, but handshakes takes too long? I hope you understand what I mean.

How do you handle those requests and all situations that may happen? Like, how do you handle a situation in which the first handshake happens immediately, but then user looses inet connection? timeout can not be applied here cause the first handshake did go through :slight_smile:

I think you would have to design for a range of times and adapt if it went outside.  Say 3G was 60s and 4G was 15s you should probably consider a failure if it hasn’t succeeded in 120s.

An invalid address would return a lookup error from DNS servers so Corona is probably handling that internally.

Makes sense. Thank you sir :slight_smile:

If a server doesn’t exist, it will error immediately. Timeouts occur when you try to connect to a valid server but it’s HTTP/HTTPS server simply isn’t responding in a timely fashion. 

Rob

Appreciate that clarification. Thanks Rob :slight_smile:

in this case Timeout is a value:

  • to use for name resolution. If resolution takes longer than this time-out value, the action is canceled. 

  • to use for server connection requests. If a connection request takes longer than this time-out value, the request is canceled. 

  • to use for sending requests. If sending a request takes longer than this time-out value, the send is canceled. 

  • to receive a response to a request. If a response takes longer than this time-out value, the request is canceled. 

I’m sure about the last two points: As I said in my op, I set the timeout to one second and send a large image as b64 string to my server in one single request. The upload took a couple of seconds and it DID go through. So, the request took longer than the time-out value and it was clearly NOT canceled. Or am I Missing something?

http 1.1 data is divided into chunks, so your large image is sending as set of many chunks and timeout applies to 1 chunk

Cool, I did not know this. Thank you very much for clarification, vitaly. Appreciate the support here :slight_smile:

One additional question: Does the event.isError get fired when timeout applies?

yes, event.isError will be ‘true’ if timeout expired,

here output from Windows 

event=

{

  “bytesEstimated”:0,

  “bytesTransferred”:0,

  “isError”:true,

  “name”:“networkRequest”,

  “phase”:“ended”,

  “requestId”:"<userdata>",

  “response”:“Timed out”,

  “responseType”:“text”,

  “status”:-1,

}

Very helpful answer, thank you sir :slight_smile:

Timeout is the time allowed for a successful handshake between a client (your app) and a server.

I might be wrong but I do not think you get a callback on a timeout but you could set a flag to false and then set it to true in your callback to let your app know it had finished a network.request() successfully.

RE no 3, you can cancel a network.request() like this https://docs.coronalabs.com/api/library/network/cancel.html

Thank you :slight_smile:

Ok, so, in other words, the timout has nothing to do with how long it may take to upload/download something AS LONG AS the first handshake took less time than whatever timeout is (default: 30 secs).

What I dont understand though: When I set the server URL to a non existing address, why does the event.isError gets fired immediately, even if timout is set to 30 seconds? How does the client know whether an URL does not exist or it does exist, but handshakes takes too long? I hope you understand what I mean.

How do you handle those requests and all situations that may happen? Like, how do you handle a situation in which the first handshake happens immediately, but then user looses inet connection? timeout can not be applied here cause the first handshake did go through :slight_smile:

I think you would have to design for a range of times and adapt if it went outside.  Say 3G was 60s and 4G was 15s you should probably consider a failure if it hasn’t succeeded in 120s.

An invalid address would return a lookup error from DNS servers so Corona is probably handling that internally.

Makes sense. Thank you sir :slight_smile:

If a server doesn’t exist, it will error immediately. Timeouts occur when you try to connect to a valid server but it’s HTTP/HTTPS server simply isn’t responding in a timely fashion. 

Rob

Appreciate that clarification. Thanks Rob :slight_smile: