Increase the idle time after connection is established in Flutter

170 views Asked by At
  1. If a connection is established, it will be closed after 15 seconds (if I am wrong please let me know the connection closing time), and the idle time cannot be extended. In my situation, multiple requests are made to the same domain and require a persistent connection, meaning the connection should be maintained even after 15 seconds. Is there another property that can be used to achieve this?

  2. Prior to establishing a connection, if multiple requests are made, each request takes time to establish a connection. Is there a way to avoid this and reduce the time taken for all requests? For instance, in the screenshot below, 4 requests are made to the same domain before the connection is established, and each request takes around 2 seconds to establish a connection. Is there a way to decrease this time?

Note: I tried using http and http_client_helper packages

enter image description here

  1. But in some cases, when a request is raised within 15 seconds of the connection established, again the connection is re-establishing, please provide some suggestions on this. Below is my code snippet using http_client_helper.

    enter image description here

Update 1

Image 1

Image 1

Image 2

Image 2

1

There are 1 answers

5
TrueKing On

Setting a timeout on the response which is a Future can help you do that. So you can have something like

final Response response = await HttpClientHelper(...).timeout(const Duration(seconds: 20));

This linked answer gives more details about handling timeout for connection and per requests. Let me know if it answers your question.


Update

As Dharanidharan pointed out, the above suggestion lets you handle the time needed to establish a connection. To manage the idleTime after connection has been established, we need to use the HttpClient class. This gives us access to low-level functionalities:

Note: HttpClient provides low-level HTTP functionality. We recommend users start with more developer-friendly and composable APIs found in package:http.

As OP stated about the idleTimeout duration, The default value is 15 seconds. But using the idleTimeout property of the HttpClient class allows us the chance to modify this value.

  HttpClient client = HttpClient();

  Future<String> get(String url) async {
    String responseJson;

    try {
      client.idleTimeout = const Duration(minutes: 5);

      // first future
      // make a request to the url
      HttpClientRequest request = await client.getUrl(Uri.parse(baseUrl + url));

      // second future
      // close the request to get the response data
      HttpClientResponse response = await request.close();

      // Process the response stream from the request
      final stringData = await response.transform(utf8.decoder).join();

      // Manage the processed response
      responseJson = _response(stringData);
    } on SocketException {
      throw FetchDataException('');
    } on ClientException {
      throw FetchDataException('');
    } catch (e) {
      setState(() {
        responseJson = e.toString();
      });
    }

    return responseJson;
  }

The above solution changes the idleTimeout from the default 15 seconds to 5 minutes. You may read more on Making a simple GET request to know the different ways to interact with the Helper class.

Here is a full code sample on Dartpad for anyone who wants a quick build test. (May not work directly in Dartpad because of import issues).